使用此代码:
#include <iostream>
#include <vector>
#include <stdio.h>
struct x
{
int a;
const char* t;
};
int main()
{
std::vector<x> instances;
while(true)
{
printf("wait for key1\n");
getchar();
getchar();
{
for(int i = 0; i < 100000; i++)
{
x n;
n.a = i;
n.t = "x instance";
instances.push_back(n);
}
//x instance deleted right?
}
{
x x1, x2, x3;
x1 = instances[0];
x2 = instances[1];
x3 = instances[2];
std::cout << x1.t << std::endl;
std::cout << x2.t << std::endl;
std::cout << x3.t << std::endl;
instances.clear();
}
printf("wait for key2\n");
getchar();
getchar();
}
return 0;
}我得到了这个输出:
wait for key2
wait for key1
x instance
x instance
x instance这很可爱,但我想我应该得到这样的输出:
wait for key2
wait for key1
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½因为必须删除x结构实例。我说错了吗?真正的实现应该是这样的:
#include <iostream>
#include <vector>
#include <stdio.h>
struct x
{
int a;
const char* t;
};
int main()
{
std::vector<x*> instances;
while(true)
{
printf("wait for key1\n");
getchar();
getchar();
{
for(int i = 0; i < 100000; i++)
{
x* n = new x();
n->a = i;
n->t = "x instance";
instances.push_back(n);
}
}
{
x* x1 = 0;
x* x2 = 0;
x* x3 = 0;
x1 = instances[0];
x2 = instances[1];
x3 = instances[2];
std::cout << x1->t << std::endl;
std::cout << x2->t << std::endl;
std::cout << x3->t << std::endl;
instances.clear(); /* delete x instances one-by-one like 'delete instances[i];' */
}
printf("wait for key2\n");
getchar();
getchar();
}
return 0;
}我对内存管理不太清楚。为什么在清理之后我还能得到(非新的)“x实例”?举个例子?
我查看了link>>,我认为必须删除for循环中的x实例吗?
更新
下面是我为其他人(像我这样的初学者)实现的示例。我将对socket数据包使用同步队列,我不关心thread.join(),仅仅因为我的线程只是工作人员,而不是管理器!(多么真实的模拟!)
#include <iostream>
#include <thread>
#include <chrono>
bool b1 = true;
bool b2 = true;
//Of course you can create only 1 boolean for all threads (isAlive should be a good name for it)
//but this way provides more detailed thread aliveness control.
void process(bool* ref, int id)
{
bool my = *ref;
while (my)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout << "thread#" << id << std::endl;
my = *ref;
}
std::cout << "thread#" << id << " end." << std::endl;
}
int main()
{
std::thread(process, &b1, 0).detach();
std::thread(process, &b2, 1).detach();
std::cin.get();
b1 = false;
std::cin.get();
b2 = false;
//MS-DOS :(
std::cin.get();
return 0;
}Posibble输出:
thread#thread#10
thread#0
thread#1
thread#1
thread#0
//Hit the enter!
thread#1
thread#0
thread#0 end.thread#1 thread#1 //按回车!thread#1 thread#1 end.//按回车!
发布于 2014-07-03 11:11:21
当您使用vector添加push_back元素时,元素将被复制到向量中。这意味着向量有自己的元素副本,即使元素被删除(不管是由您还是在范围的末尾),向量仍然包含元素。当该向量被删除时,该向量负责逐个删除它的元素,所有这些都是由它自己完成的,所以您不必这样做。
here
在向量的末尾,在当前的最后一个元素之后添加一个新元素。val的内容被复制(或移动)到新元素。
您的“改进”版本(动态分配变量)是指针的误用。如果您所做的只是将元素插入到vector中,那么就不需要动态分配它们,因为向量甚至不会包含动态分配的值--只包含它们的副本。
发布于 2014-07-03 11:11:45
你在向量里放了一份。
for(int i = 0; i < 100000; i++)
{
x n;
n.a = i;
n.t = "x instance";
instances.push_back(n); // <--- Copy of n is created and added to instances
}发布于 2014-07-03 11:12:18
在执行push_back时,将实例的副本插入到向量中。因此,删除原始实例并不重要。字符串可能会丢失,因为它们是在本地分配的,原始字符串被销毁了。
对于未被删除的字符串,可能是因为您向指针分配了一个静态字符串。对动态创建的字符串也进行同样的尝试。
https://stackoverflow.com/questions/24552048
复制相似问题