是否需要销毁char * ="string"?还是char * =新的char [6]? [英] Is there a need to destroy char * = "string" or char * = new char[6]?

查看:141
本文介绍了是否需要销毁char * ="string"?还是char * =新的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 ++中)自己手动分配内存时,只需要手动释放字符串即可.如果不使用mallocnew,则会在堆栈上或作为编译时常量创建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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆