Why is the code snippet below not crashing even though I deleted the object?

class object { public: void check() { std::cout<<"I am doing ok..."<<std::endl; } }; int main() { object *p = new object; p->check(); delete p; p->check(); delete p; p->check(); } 

EDIT : Guru, I am confused by many of the statements “it may fail or may not be” .. why there is no standard to say how we deal with a block of memory that is deleted using the “delete operator” ..? Any inputs?

+7
c ++
source share
10 answers

Since it actually looks after the compiler had its own path, it is something like this:

 object::check( object* this ) { // do stuff without using this } int main() { object *p = new object; object::check( p ); delete p; object::check( p ); delete p; object::check( p ); } 

Since you do not touch "this", you are not actually accessing bad memory.

Even though removing p twice should crash:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.2

+10
source share

Since the function does nothing with the data of the object-object or the this pointer.

It's like a function call

 void check(object *self) { std::cout<<"I am doing ok..."<<std::endl; } 

with an invalid pointer as an argument to self .

There is a double removal that may be damaged in some environments.

+7
source share

Delete only frees memory and returns it to the heap.

The value of the pointer is undefined after the deletion was called, so this could lead to its crash.

The programming tip that I use to reduce programming errors is that after deleting, set the pointer to NULL. Thus, you know that you do not accidentally use the pointer after deleting it.

+7
source share

You can do the same with nil pointers until you never access the state of the class instance:

 class object { public: void check() { std::cout<<"I am doing ok..."<<std::endl; } }; int main() { object *p = 0; p->check(); } 
+5
source share

This is even funny. This compiles and works easily:

 #include <iostream> using namespace std; class object { public: void check() { cout << "I am doing ok..." << endl; } }; int main() { object *p = (object*)0; p->check(); return 0; } 

In the shell:

 $ g ++ -ot t.cc
 $ ./t
 I am doing ok ...
 $

:) Actually, you do not have an object to call this method! greetings h

+5
source share

Even if the method call used this pointer, this would not guarantee failure. The pointer p is not reset when deleted, so it still points to the same address in memory. Depending on the implementation of new / delete and the heap manager, this memory may not even be reused, so using p may continue to work even when freeing memory.

+2
source share

1) Compiler dependent on Mac OS with gcc 4.0.1:

 g++ -Wall -g -c main.cpp -o main.o g++ -ox main.o ./x I am doing ok ... I am doing ok ... x(5857) malloc: *** error for object 0x100150: double free *** set a breakpoint in malloc_error_break to debug I am doing ok ... 

Double free causes problems.

2) Usually, deleting a pointer that has already been deleted is not indicated, you should always point to 0 after deletion, allowing the pointer to be deleted with a value of 0.

3) The reason that it can still print a line is because the pointer still points to freed memory, but I would assume that only the pointer is fixed, for example. It is returned to the free pool for reuse, the memory is not overwritten or duplicated, so if you search for a pointer, you will still get the original values, unless the memory is reused.

+1
source share

delete does not set p to null, so it still points to a memory location. It depends on the implementation of new / delete, but usually only deletes the labels of the part of memory that is available for any new allocations.

It is like you are deleting a file from your hard drive. The file system simply marks this area as available. File data exists and can be restored until you make new entries.

+1
source share

I think it depends on the environment (platform / compiler) ...
This at least gives unexpected behavior. I think you are lucky that this is not a failure in your case; -)

0
source share

To add a little to what others have said, try using a member variable of your class inside the check () method, and then see what happens.

A function call in the case of classes is similar to a normal function call, with the exception of one small difference. Arguments are pushed onto the stack in both cases, but when the function of the object is called, 'this' is pushed onto the stack as the first argument. This 'this' is used inside the function to access member variables. Since you are not accessing any member variables, it works. And yes, it can work even if you access the member variables, but you get unexpected behavior because the compiler can use the memory allocated by "p". As soon as the compiler uses this memory, you will begin to get crashes. For example, if you declare additional variables after deletion before reusing "p", it may crash.

Edit: Also, if your method does not have access to any member variable, then the static method is the specific candidate.

0
source share

All Articles