char *和delete问题 [英] Problems with char * and delete

查看:91
本文介绍了char *和delete问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试取消分配此指针时,我遇到了棘手的问题.

I have tough problems while trying to de-allocate this pointer.

我遇到此错误:

对象0x10007fd20错误:未分配释放的指针

error for object 0x10007fd20: pointer being freed was not allocated

我具有以下结构:

char * doSomething() //I cannot change this method.
{
     return "hello world";
};
int main ()
{
  char * var= doSomething();
  cout<< var<<endl;
  delete[] var;
};

请考虑我不能使用字符串,因为 doSomething方法没有提供给我.由于我经常使用它,..一段时间后,由于RAM内存的使用,应用程序崩溃了.

Please consider that I cannot use string, because I'm not being provided with the doSomething method. As I'm using it a lot.. after a while the app collapses because of the RAM memory use.

推荐答案

您只能删除使用相应的 operator new 分配的内容.字符串文字具有静态存储期限.它们不是动态分配的.根据C ++标准

You can delete only what was allocated using the corresponding operator new. String literals have static storage duration. They are not dynamically allocated. According to the C++ Standard

还引用了8个普通字符串文字和UTF-8字符串文字作为狭窄的字符串文字.窄字符串文字的类型为数组n const char",其中n是如下定义的字符串的大小,并且具有静态存储期限(3.7).

这篇关于char *和delete问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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