内存管理基础
Memory management basics?
使用下面的代码:
#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 key1n");
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 key2n");
getchar();
getchar();
}
return 0;
}
我得到这样的输出:
wait for key2
wait for key1
x instance
x instance
x instance
这很可爱,但我认为我应该得到这样的输出:
wait for key2
wait for key1
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½
因为必须删除x个struct实例。我错了吗?真正的实现应该是这样的:
#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 key1n");
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 key2n");
getchar();
getchar();
}
return 0;
}
我不清楚内存管理。为什么我还能得到(非新)清理后的"x个实例"?例子吗?
我已经在link>>并且我认为必须删除for循环中的x个实例?
<标题> 更新这是我为其他人(像我这样的初学者)提供的示例实现。我将使用套接字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号结束。//按回车键!
标题>当您使用push_back
向vector
添加元素时,该元素将复制到vector中。这意味着vector有自己的元素副本,即使元素被删除(由您删除或在作用域结束时删除),vector仍然包含该元素。当vector对象被删除时,vector对象会自己一个一个地删除它的元素,所以你不必这样做。
您可以在这里找到更多关于push_back
的信息:
在vector对象的最后一个元素之后,在vector对象的末尾添加一个新元素。val的内容被复制(或移动)到新元素中。
你的"改进"版本,动态分配变量,是对指针的误用。如果您所做的只是将元素插入到vector
中,那么就不需要动态分配它们,因为向量甚至不包含动态分配的值-仅包含它们的副本。
您正在将副本放入向量中。
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
}
在执行push_back操作时,将实例的副本插入到vector中。因此,删除原始实例并不重要。这些字符串可能会丢失,因为它们是在本地分配的,原始字符串被销毁了。
关于未被删除的字符串,可能是由于您为指针分配了静态字符串。
不,不应该删除。
在vector对象上调用push_back()时,等于在vector对象内部创建了一个x的副本。
std::vector::clear()不删除项,而是调用任何适当的析构函数。实例的元素是指针,没有全局适用的析构函数。虽然您可能认为删除是一种适当的行为,但std::vector::clear()并不知道谁可能拥有这些指针。一般来说,如果分配了内存,就应该释放它。试着
for( std::vector<x*>::iterator it=instances.begin(); it!=instances.end(); ++it)
{
delete *it;
}
instances.clear();
- 当vector是tje全局变量时,c++中vector的内存管理
- 当分配一个字符串值并稍后通过分配另一个值进行更改时C++如何管理内存?
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 可以通过非原始指针来增强容器矢量管理内存
- 如何使用 std::vector<std::tuple<A,B>> 来管理内存(调整大小、保留,...),但实际上将 As 保留在 B 之前,连续
- 线程管理内存泄漏
- 通过读取文件创建映射<字符串,矢量>时如何管理内存<string>
- 管理内存C++
- ptr_vector如何管理内存
- 在C++Builder/Firemonkey中使用表单创建来管理内存
- 如何正确管理内存(运行时)C++
- 在Node.js中使用Native Abstractions时,如何管理内存
- ostream是如何管理内存的
- C++中管理内存泄漏的问题
- OpenCL:在 CPU 上而不是在 GPU 上更正结果:如何正确管理内存
- deque是如何管理内存的
- 用c++/cli互操作管理内存
- 如何衡量管理内存所花费的时间
- (加速C++)章节管理内存
- 如何在此场景中管理内存