C字符串删除 [英] C strings deletion in C++

查看:95
本文介绍了C字符串删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么好处,出了什么问题?在这种情况下,我必须调用删除,以防止内存泄漏?
另外,C和C ++中的行为是否完全相同?是否有任何差异?

What's good and what's wrong? In which case do I have to call delete in order to prevent memory leak? Also, is the behaviour exactly the same in C and C++? Are there any differences?

const char* a = "blahblah";
...
delete a;

char b* = new char('a');
...
delete b;

char c[100] = "blahblah";
...
delete c;

char d* = new char[40];
...
delete d;

char e* = new char[40];
...
delete[] e;


推荐答案


我必须调用delete才能防止内存泄漏。

In which case do I have to call delete in order to prevent memory leak

只有 delete 你从 delete [] 得到从 new [] 。每一个其他删除都是错误的。

Only delete what you got from new and delete [] what you got from new []. Every other delete is wrong.

这篇关于C字符串删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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