Deleted pointer causes undefined behaviour
这文章是早期闲得用英文写的,删除了怪可惜的,贴出来证明自己会英文算了。。。
Occasionally,on stackoverflow.com,I found a classic problem which is age-old but never to be out-dated,that is,the undefined behaviour occurred by reusing the pointer which was just deleted.
The code below may be a good example.(This piece of code refers to here)
1 #include <iostream>
2 class MyClass
3 {
4 public:
5 int a;
6 MyClass() : a(10)
7 {
8 std::cout << "constructor rann";
9 }
10 void method(std::string input_)
11 {
12 std::cout << input_.c_str()<< "n";
13 }
14 ~MyClass()
15 {
16 std::cout << "destructor rann";
17 }
18 };
19 int main()
20 {
21 MyClass* ptr = new MyClass;
22 ptr->method("1");
23 delete ptr;
24 ptr->method("2.5");
25 }
26
I used CL.exe(Version 17.00) and g++(Version 4.6) to run this code.
This code outputs:
constructor ran
1
destructor ran
2.5
Actually,I also confronted a confusing problem like this one when I was a c++ beginner(Until now,I’m still green at c++).I fear that I couldn’t believe what happened if I didn’t test what the value of variable a was.
So I added
cout<<a<<endl
to line 27 and this code outputs:
Constructor ran
1
destructor ran
2.5
-17891602
At that time, I realized that the memory allocated to ptr was actually deleted.
Well...now,let us look at the definition of delete from Wiki:
"The delete operator calls the destructor of the given argument, and returns memory allocated by new back to the heap.",and the description of deallocation from N3376:
"If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value, the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage. The effect of using an invalid pointer value is undefined."
As for our program,after deleting the storage referenced by ptr, the manipulation on ptr is undefined.Under our current condition,ptr points to the same data it pointed to before and calling methods on it just works “fine”, but it’s not what we want and what we expect because maybe at next second,the allocator will allocate this memory to someone else.So,there is a recommended common method to deal with this counterintuitive problem:
delete ptr;
ptr = NULL;
In this way,you could ensure that you won’t incautiously reuse this pointer before a piece of new memory is reallocate-d to it.
In conclusion,I suggest that you shouldn’t rely on pointer type unless it’s necessary.In most cases,reference is enough for us to solve problems.
Deleted pointer causes undefined behaviour的更多相关文章
- C语言undefined behaviour未定义行为
C语言中的未定义行为(Undefined Behavior)是指C语言标准未做规定的行为.同时,标准也从没要求编译器判断未定义行为,所以这些行为有编译器自行处理,在不同的编译器可能会产生不同的结果,又 ...
- Pointer arithmetic for void pointer in C
http://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c When a pointer t ...
- [Elixir009]像GenServer一样用behaviour来规范接口
1.Behaviour介绍 Erlang/Elixir的Behaviour类似于其它语言中的接口(interfaces),本质就是在指定behaviours的模块中强制要求导出一些指定的函数,否则编译 ...
- zookeeper 学习笔记 (C语言版本)
1.zookeeper简介 zookeeper是Hadoop的子项目,在大型分布式系统中,zookeeper封装好了一些复杂易出错的服务,提供简单易用的接口,给使用者提供高效稳定的服务.这些服务包括配 ...
- [搬运]CORBA中BOA和POA的含义
先来BOA,搬自:http://www.cl.cam.ac.uk/research/dtg/attarchive/omniORB/doc/2.8/omniORB2/node6.html The Bas ...
- (C/C++) Interview in English - Points.
Q: What is a dangling pointer? A: A dangling pointer arises when you use the address of an object af ...
- Why am I able to change the contents of const char *ptr?
http://stackoverflow.com/questions/3228664/why-am-i-able-to-change-the-contents-of-const-char-ptr I ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- [转]ARM/Thumb2PortingHowto
src: https://wiki.edubuntu.org/ARM/Thumb2PortingHowto#ARM_Assembler_Overview When you see some assem ...
随机推荐
- Your build settings specify a provisioning profile with the UUID, no such provisioning profile was found的解决方案
在Archive项目时,出现了“Your build settings specify a provisioning profile with the UUID “”, however, no suc ...
- 【C#】开发可以可视化操作的windows服务
使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一直执行我们开发的某些程序的时候.这里我以一个WCF的监听服务为 ...
- 【笔记】让DIV水平垂直居中的两种方法
今天写的了百度前端学院春季班的任务:定位和居中问题 由于距离上次学习CSS有点久远了,加上以前木有记笔记的习惯,方法忘得只剩下一种,今天通过网上查阅资料总结了以下两种简单的方法让DIV水平垂直居中. ...
- JS赋值传递的问题
根据值的类型是基本类型值还是复杂类型的值在传递时会有不同. JS函数的参数传递是按值传递,基本类型值传递的是副本,复杂类型值传递的是引用.从而会影响原来的值,不会改变原来的复制前的引用. functi ...
- hdu 1908
题意:一个人有一个代号K和一个优先等级P,一共有4种操作,0:结束,1:新建一个人,代号为K,优先等级为P,2:输出优先等级最大的人的代码,并删除该人,3:输出优先等级最小的人的代码,并删除该人. 用 ...
- Android Studio 打包流程
(1)Android Studio菜单Build->Generate Signed APK (2)弹出窗口 (3)创建密钥库及密钥,创建后会自动选择刚创建的密钥库和密钥(已拥有密钥库跳过) ...
- 模拟app上商品详情点击图片放大并且可以切换大图
主要使用swiper插件,这里使用各小技巧,就是用两个swiper容器,点击一个小图容器,去让大图容器展示出来 小图容器 <div class="q_banner"> ...
- mysql性能优化学习笔记(2)如何发现有问题的sql
一.使用mysql慢查询日志对有效率问题的sql进行监控 1)开启慢查询 show variables like ‘slow_query_log’;//查看是否开启慢查询日志 ...
- MySQLdb-python的安装
第一步下载: 第一步:进入https://github.com/farcepest/MySQLdb1/ 第二步:解压 Shell>unzip /root/MySQLdb1-MySQLdb-1.3 ...
- tracker-store and tracker-miner-fs eating up my CPU on every startup
Tracker is a synergy of technologies that are designed to provide a highly sophisticated, innovative ...