是否有必要销毁char * =“string”或char * = new char [6] [英] Is there a need to destroy char * = "string" or char * = new char[6]
问题描述
我假设当 char * =string
与 char * = new char [6]
。我相信这些字符串是创建在堆而不是堆栈。所以,当我使用它们或者他们被自己毁灭时,我需要销毁它们或释放它们的记忆。
I assume when I do char* = "string"
is the same thing aschar* = 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 * = new char [6]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!