[Digikam-devel] delete does not reset the pointer in GCC (4.6.0)

Andi Clemens andi.clemens at gmx.net
Wed Jun 15 08:59:37 BST 2011


Hi,

some time ago we removed all lines that looked like that

delete xyz;
xyz = 0;

by just

delete xyz;

because GCC resets the pointer to zero.

But actually it doesn't seem to do that. I was testing std::auto_ptr for  
another project and accidentally added a
delete xyz
twice.

The program crashed. I wrote a simple test program to verify this, and GCC  
never resets the pointer:


#include <iostream>
#include <memory>

class MyBase {
public:
	virtual bool doSomething() {
		std::cout << "done something...!" << std::endl;
		return true;
	}
};

int main() {
	MyBase* my = new MyBase();
	delete my;
	std::cout << "pointer: " << my << std::endl;
	if (my  == 0)
	{
		std::cout << "aha" << std::endl;
	}

	return 0;
}


The program never reaches the if block and the cout gives me an actual  
pointer address.
Is it a bug in GCC 4.6.0 or does GCC really not reset the pointer.

As we made the change months ago, I said that some compilers do not reset  
the pointer, somebody meant that every known compiler does.
Well GCC doesn't (anymore)  :-)


Can you confirm this?

Andi

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/



More information about the Digikam-devel mailing list