PostHeaderIcon zero pointer to avoid reuse

Sometimes we use pointers, after a pointer is deleted(or freed) it should not be used, that doesn’t prevent bad code from using it though, you know pointers can cause unknown problems, it also one of the toughest problems to debug.

Here’s an example of what were talking about:

	int* data;
	data=new int[10];
	// ...

	delete[] data;

	// ...
	// memory corruption

You should set the pointer to NULL after deleting or free it.

	delete[] data;

The result is that on most systems if you try and use the pointer, the system will crash.

	// ...thousand lines of code
	data[2]=2; // system crashes

In this way it does not prevent errors but change them, accessing a memory pointer after it is deleted can result in data corruption or heap corruption. The results of this error may not show up for a long time(especially big memory today) and will probably happen when you are executing a totally unrelated piece of code.
So remember reset pointer after each use.


Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by