使用此代码:
#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;
}
我不清楚内存管理。为什么我仍然可以获得(非新的&#39; d)x实例&#39;清理后?实例
我已查看link>>,我认为必须删除for循环中的x个实例?
这是我为其他人(像我这样的初学者)的示例实现。 我将使用sync io队列作为套接字io数据包和 我不关心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.
线#1 线#1 //点击输入! 线#1 线程#1结束。 //点击输入!
答案 0 :(得分:3)
当您使用vector
向push_back
添加元素时,该元素将复制到该向量中。这意味着向量具有自己的元素副本,即使元素被删除(由您或范围的末尾),向量仍包含元素。
当向量被删除时,向量会逐个删除它的元素,所有这些都是自己的,所以你不必这样做。
You can find more information about push_back
here:
在向量的末尾添加一个新元素,在其当前的最后一个元素之后。 val的内容被复制(或移动)到新元素。
您动态分配变量的“改进”版本是对指针的误用。如果你所做的只是将元素插入到vector
中,则不需要动态分配它们,因为向量甚至不包含动态分配的值 - 只包含它们的副本。
答案 1 :(得分:1)
您正在将副本放入矢量中。
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
}
答案 2 :(得分:0)
当你执行push_back时,将实例的副本插入向量中。因此删除原始实例并不重要。可能会丢失字符串,因为它们是在本地分配的,原始字符串已被销毁。
关于未被删除的字符串,可能是因为您为指针分配了一个静态字符串。尝试使用动态创建的字符串。
答案 3 :(得分:0)
在向量上调用push_back()时,您将在向量内创建x的副本。
答案 4 :(得分:0)
std :: vector :: clear()不会删除项目,而是调用任何适当的析构函数。 实例的元素是指针,没有全局适用的析构函数。虽然你可能认为删除是一个合适的行为std :: vector :: clear()不知道谁可能拥有这些指针。一般情况下,如果你分配了内存,你应该释放它。 尝试
for( std::vector<x*>::iterator it=instances.begin(); it!=instances.end(); ++it)
{
delete *it;
}
instances.clear();