C字符串缺失C ++ [英] C strings deletion in C++
本文介绍了C字符串缺失C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是好,什么是错的?在这种情况下,我必须打电话,以便删除对prevent内存泄漏?
此外,在C和C ++的行为完全一样?是否有什么不同吗?
为const char * A =blahblah;
...
删除;字符B * =新的字符('A');
...
删除B:焦C [100] =blahblah;
...
删除C;字符D * =新的char [40];
...
删除D组;焦炭E * =新的char [40];
...
删除[我们];
解决方案
在这种情况下,我必须打电话,以便删除对prevent内存泄漏
块引用>只有
删除
您从新
和删除[] $ 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;
解决方案In which case do I have to call delete in order to prevent memory leak
Only
delete
what you got fromnew
anddelete []
what you got fromnew []
. Every other delete is wrong.这篇关于C字符串缺失C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文