是否需要销毁char * ="string"?还是char * =新的char [6]? [英] Is there a need to destroy char * = "string" or char * = new char[6]?
问题描述
我假定char* = "string"
与char* = new char[6]
相同.我相信这些字符串是在堆而不是堆栈上创建的.那么,使用完它们后,我是否需要销毁它们或释放它们的内存还是它们自己销毁了?
I assume that char* = "string"
is the same to char* = new char[6]
. I believe these strings are created on the heap instead of the stack. So do I need to destroy them or free their memory when I'm done using them or do they get destroyed by themselves?
推荐答案
否.使用malloc
函数(在C中)或new
运算符(在C ++中)自己手动分配内存时,只需要手动释放字符串即可.如果不使用malloc
或new
,则会在堆栈上或作为编译时常量创建char*
或字符串.
No. You only need to manually free strings when you manually allocate the memory yourself using the malloc
function (in C) or the new
operator (in C++). If you do not use malloc
or new
, then the char*
or string will be created on the stack or as a compile-time constant.
这篇关于是否需要销毁char * ="string"?还是char * =新的char [6]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!