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. 采用Filter的方法解决Servlet的编码问题

    这样比你自己在Servlet代码中硬编码request.setCharacterEncoding, response.setCharacterEncoding方便多了 总之,如果你添加了这个filte ...

  2. Eclipse下把jar包放到工程lib下和通过buildpath加载有什么不同(解决找不到类的中级方法)

    我通过Eclipse的 User Libranry 将jar导入 Eclipse里面,编译没有问题,运行的时候就报class文件没有定义,后来上网上查了下,原因找到啦,是这样的: 用Java Buil ...

  3. mysql的OFFSET实现分页

    使用limit 可以实现分页比如 limit 0,5  是从1到5条, limit 5,5  是从,6到10条, 使用limit offset 时 limit 5 offset 0 从 1 到5 条 ...

  4. 【Unity】用代码给按钮动态添加点击事件

    问题:多数情况下用UGUI的Button控件身上的OnClick()列表可以指明该按钮点击后触发的回调.现在想要调用自定义脚本里的方法,当这个脚本挂在Button所属的Canvas身上时,传入Canv ...

  5. kernel 生成uImage

    有些u-boot 指定要链接uImage 所以今天记录一下kernel 生成uImage 的条件 在kernel 配置完make menuconfig 后,进行编译,发现make uImage 生成不 ...

  6. 一、VM8.0 + CentOS 6.5 连接网络

    VM8.0 + CentOS 6.5 连接网络

  7. CodeIgniter(3.1.4)框架中成功/错误跳转

    if ( ! function_exists('error')) { /** * 错误跳转 */ function error($mes, $url) { echo '<script type= ...

  8. LoadRunner性能分析指标解释

    Transactions(用户事务分析) 用户事务分析是站在用户角度进行的基础性能分析. 1.Transation Sunmmary(事务综述) 对事务进行综合分析是性能分析的第一步,通过分析测试时间 ...

  9. DSSM(DEEP STRUCTURED SEMANTIC MODELS)

    Huang, Po-Sen, et al. "Learning deep structured semantic models for web search using clickthrou ...

  10. ubuntu 16.04 安装pycharm

    Ubuntu16.04下安装Cuda8.0+Caffe+TensorFlow-gpu+Pycharm过程(Simple) ubuntu 16.04 安装pycharm 1.安装java  jdk 直接 ...