动态内存分配需要注意什么?
What are the things need to be taken care while dynamic memory allocation?
当我使用malloc()
或new
分配内存时,有时会出现运行时间错误,如何避免这些错误?
你malloc()
,你需要free()
。
你new
,你需要delete
。
你new []
,你需要delete []
。
任何其他组合都是未定义的行为。
此外,new
和 new []
实际上在分配的内存中构造对象(delete
/ delete[]
调用其析构函数),而 malloc()
/ free()
不这样做——它们只处理内存,而不是对象。
鉴于所提供的(缺乏)信息,我能帮到的就这么多了。
当我使用malloc()
或new
分配内存时,有时会出现运行时间错误,如何避免这些错误?
你malloc()
,你需要free()
。
你new
,你需要delete
。
你new []
,你需要delete []
。
任何其他组合都是未定义的行为。
此外,new
和 new []
实际上在分配的内存中构造对象(delete
/ delete[]
调用其析构函数),而 malloc()
/ free()
不这样做——它们只处理内存,而不是对象。
鉴于所提供的(缺乏)信息,我能帮到的就这么多了。