[英]Which of these pointer need free() or delete()
I'm confused with freeing or deleting pointer. 我对释放或删除指针感到困惑。 For what I have read I have to use free with malloc , calloc , realloc and delete with new
对于我阅读的内容,我必须将free与malloc,calloc,realloc和new一起使用
But what if : 但是如果:
char * hello = "helloworld"; //need to use free()?
char * pointerWmalloc= (char*) malloc(sizeof(char));
pointerWmalloc="This need free for sure";
//here need to use free() am I right ?
char * dupOfHello = strdup(hello); //need to use free()?
char * thisWnew= new Char(); // this need delete for sure
char * charReturnedByFunc= returnchar(); // need to use free() for charReturnedByFunc??
char * hello = "helloworld"; //need to use free()?
"helloworld"
is a const char*
and hello
is now a dynamic reference to it. "helloworld"
是一个const char*
而hello
现在是对其的动态引用。 (You will trigger a runtime exception if you try to change any characters in the string, or free
this memory) (如果尝试更改字符串中的任何字符或
free
此内存,则将触发运行时异常)
char * pointerWmalloc = (char*)malloc(sizeof(char));
This allocated memory needs to be free
'd BEFORE reassignment to 在重新分配给之前,此分配的内存需要
free
pointerWmalloc = "This need free for sure";
which like hello
now points to a const char
* with the same restrictions. 像
hello
现在指向具有相同限制的const char
*。
char * dupOfHello = strdup(hello); //need to use free()?
this needs to be free
'd 这需要
free
char * thisWnew = new Char(); // this need delete for sure
This does need delete
. 这确实需要
delete
。 Also, Char
is not a class unless you make it one, but I think you meant char()
另外,除非您将
Char
一个类,否则它不是一个类,但是我认为您的意思是char()
char * charReturnedByFunc = returnchar(); // need to use free() for charReturnedByFunc??
This one is tricky, it completely depends on what returnchar() returns a pointer to. 这是一个棘手的问题,它完全取决于returnchar()返回的指针。 A
const char*
? const char*
? no. 没有。 A
char*
owned by another object or function? 另一个对象或函数拥有的
char*
? Maybe. 也许。 A
char*
allocated inside the function? 函数内部分配的
char*
? Maybe. 也许。 Unless you make it completely clear, you can't know if
returnchar()
returns a malloc
'd or new
'd char*
. 除非你让它完全清楚,你可以不知道如果
returnchar()
返回malloc
“d或new
” d char*
。
All this and more is why it's recommended to avoid raw pointers in favor of std::unique_ptr
and std::shared_ptr
, avoid malloc
in favor of new
, and avoid char*
in favor of std::string
. 所有这些以及更多的原因是为什么建议避免使用原始指针来支持
std::unique_ptr
和std::shared_ptr
,避免使用malloc
来支持new
,并避免使用char*
来支持std::string
。
I'll answer with comments in the code below: 我将在下面的代码中用注释回答:
char * hello = "helloworld"; // Do not free/delete pointer
// `hello` points to read-only memory
char * pointerWmalloc= (char*) malloc(sizeof(char)); // use free(pointerWmalloc) here
pointerWmalloc="This need free for sure"; // now you created a memory leak since pointerWmalloc now points to read-only memory. So do not free/delete
char * dupOfHello = strdup(hello); // use free(dupOfHello) here (as documented)
char * thisWnew= new char(); // use delete thisWnew here
char * charReturnedByFunc= returnchar(); // need to read the documentation.
// could be free, delete, none of the two or something else
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.