本文介绍了删除运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 考虑一下: char * ptr =" a string; ptr是一个指向动态创建对象的指针,我必须使用 delete运算符来释放内存吗?我确定一根绳子 part创建 一个字符数组,并返回指向第一个元素的指针。 问候 wert --- 外发邮件经过无病毒认证。 由AVG反病毒系统检查( http://www.grisoft.com)。 版本:6.0.518 /病毒库:316 - 发布日期:11/09/2003Hi,Consider this:char* ptr = "a string";Is ptr a pointer to a dynamically created object for which i must use thedelete operator to deallocate memory? I''m sure the "a string" part createsan array of characters, and returns a pointer to the first element.Regardswert---Outgoing mail is certified Virus Free.Checked by AVG anti-virus system (http://www.grisoft.com).Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003推荐答案 ptr绝对不是动态创建的对象,也不是字符串。如果你试图删除ptr,那么会导致运行时错误,或者 内存管理器的空闲列表将被搞砸。 文件字符串和其他静态对象的内存是在加载 程序时分配的,并且不在动态分配的内存中。ptr is definitely NOT a dynamically created object, nor is "a string". Ifyou attempt to delete ptr, either a run time error will result, or thememory manager''s free list will be messed up.Memory for literal strings and other ''static'' objects is allocated when theprogram is loaded, and is not in the realm of dynamically allocated memory. 是的,但数组是由编译器创建的,并且是静态分配的。 你不应该删除它。Yes, but the array is created by the compiler and is statically allocated.You should not delete it. 时,分配其他静态对象,并且不在动态分配the program is loaded, and is not in the realm of dynamically allocated 内存的范围内。 我认为编译器会在堆栈上为字符串 literal创建一个字符数组,因此不需要使用delete运算符。 问候BNmemory.I gather that the compiler creates an array of characters for the stringliteral on the stack, and thus there is no need to use the delete operator.Regards BN 这篇关于删除运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!