1. When you created an array and want to return the memory to system. You need to explicitly add [] to show that that's an array you want to return, so more than one destructors will be called.

2. However, things might not be that simple. Imagine you are fixing a bug and trying to delete an object someone else wrote, for example

std::string *foo = new Foo;

How will you delete it? Pretty much everyone would write

detele foo;

But that is not necessary right until you look a little bit further and come across this line:

typedef std::string Foo[];

Oops, Foo is actually an array of string, so you gonna need to write

delete foo[];

So, what lesson have you learned from here?

One is that don't typedef array. And the other is to use STL library instead of dynamic allocated memory unless it's absolutly necessary.

Effective C++ Item 16 Use the same form in corresponding uses of new and delete的更多相关文章

  1. 读书笔记 effective c++ Item 16 成对使用new和delete时要用相同的形式

    1. 一个错误释放内存的例子 下面的场景会有什么错? std::]; ... delete stringArray 一切看上去都是有序的.new匹配了一个delete.但有一些地方确实是错了.程序的行 ...

  2. 读书笔记 effective c++ Item 52 如果你实现了placement new,你也要实现placement delete

    1. 调用普通版本的operator new抛出异常会发生什么? Placement new和placement delete不是C++动物园中最常遇到的猛兽,所以你不用担心你对它们不熟悉.当你像下面 ...

  3. 读书笔记 effective c++ Item 31 把文件之间的编译依赖降到最低

    1. 牵一发而动全身 现在开始进入你的C++程序,你对你的类实现做了一个很小的改动.注意,不是接口,只是实现:一个私有的stuff.然后你需要rebuild你的程序,计算着这个build应该几秒钟就足 ...

  4. 读书笔记 effective c++ Item 51 实现new和delete的时候要遵守约定

    Item 50中解释了在什么情况下你可能想实现自己版本的operator new和operator delete,但是没有解释当你实现的时候需要遵守的约定.遵守这些规则并不是很困难,但是它们其中有一些 ...

  5. Effective STL 学习笔记 Item 16:vector, string & C API

    有时需要支持 C 的接口,但这并不复杂. 对于 vector 来讲, \(v[0]\) 的地址 \(\&v[0]\) 即可作为数组指针传递给 C API: 1: // Legacy C API ...

  6. Effective JavaScript Item 21 使用apply方法调用函数以传入可变參数列表

    本系列作为Effective JavaScript的读书笔记. 以下是一个拥有可变參数列表的方法的典型样例: average(1, 2, 3); // 2 average(1); // 1 avera ...

  7. 读书笔记 effective c++ Item 13 用对象来管理资源

    1.不要手动释放从函数返回的堆资源 假设你正在处理一个模拟Investment的程序库,不同的Investmetn类型从Investment基类继承而来, class Investment { ... ...

  8. 读书笔记 effective c++ Item 54 让你自己熟悉包括TR1在内的标准库

    1. C++0x的历史渊源 C++标准——也就是定义语言的文档和程序库——在1998被批准.在2003年,一个小的“修复bug”版本被发布.然而标准委员会仍然在继续他们的工作,一个“2.0版本”的C+ ...

  9. Item 16: 让const成员函数做到线程安全

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果我们在数学领域里工作,我们可能会发现用一个类来表示多项式会很方 ...

随机推荐

  1. 【转】批量删除redis中的key

    1. DEL 直接加键名称 DEL key1 key2 key3 127.0.0.1:6379>  DEL site_msg_99973  false site_msg_99974   fals ...

  2. elasticsearch的python增删查改实例分析

    Reference:  http://bigg.top/2015/11/29/elasticsearch%E7%9A%84python%E5%A2%9E%E5%88%A0%E6%9F%A5%E6%94 ...

  3. Java 命令行运行java程序,出现“找不到或无法加载主类 ”的注意事项

    引用:http://blog.chinaunix.net/uid-27106528-id-5209914.html 要在CMD命令行中使用java 运行java程序,关于出现 “找不到或无法加载主类 ...

  4. 【ExtJS】各种获取元素组件方法

    1.get().getDom().getCmp().getBody().getDoc(): get(id/obj): get方法用来得到一个Ext元素,也就是类型为Ext.Element的对象, Ex ...

  5. [转]wait,notify,notifyAll,join,yield,sleep的区别和联系

    1.  Thread.sleep(long) 和Thread.yield()都是Thread类的静态方法,在调用的时候都是Thread.sleep(long)/Thread.yield()的方式进行调 ...

  6. iOS边练边学--UITableViewCell的常见属性设置

    // 取消选中的样式(常用) 让当前 cell 按下无反应 cell.selectionStyle = UITableViewCellSelectionStyleNone; // 设置选中的背景色,U ...

  7. BuddyPress创建组、查看成员信息等找不到页面

    BuddyPress创建组.查看成员信息等找不到页面 http://aoxuangame.com/wordpress/groups/create/ http://aoxuangame.com/word ...

  8. eclipse新建python项Project interpreter not specified

    安装好pydev后新建python项目时提示”Project interpreter not specified“的错误,这是因为没有导入python开发环境所致 解决方法如下:1.找到eclipse ...

  9. jQuery实现布局高宽自适应

    在页面布局(layout)时经常是上左右的框架布局并且需要宽.高度的自适应,div+css是无法实现(*hegz:div+css其实是可以实现的,利用jQuery比较容易实现浏览器的兼容性),所以需要 ...

  10. loadrunner循环执行某个动作

    1.action部分定义  int   i;  int   count; 2. 打算循环的代码前代码如下:   count=rand() % 8 +1;       for(i=0;i<coun ...