Doubt regarding memory deallocation...

S

Sachin Midha

Hi,
I just saw someone code something like this...
int y;
class a{
public:
int x;
void f(){
y=1;
delete this;
g();
x=5;
}
void g(){}
};

Can someone please let me know what consequences would {delete this}
have on the code following that line?
What I feel is that, since functions are not allocated memory(shared
between all the objects of a class), call to g() would be done
successfully, but the code x=5, would cause a segmentation fault,
since it has no memory allocated(since object has been deleted).
But then, somebody told me that if I look at the assembly level,
delete calls a routine, and when it returns, it is lost & causes a
crash in the application. So, I am really confused, please help me
understand this.

Thanks,
Sachin
 
J

Juha Nieminen

Sachin Midha said:
but the code x=5, would cause a segmentation fault

Actually in most systems it wouldn't. Just because you delete an object
doesn't mean that the memory block where the object was will be released
to the operating system by the memory allocator. (A segmentation fault
happens if a process tries to access memory that it didn't get from the OS.)

It's still undefined behavior, though, so anything could happen.
 
S

Sachin Midha

  Actually in most systems it wouldn't. Just because you delete an object
doesn't mean that the memory block where the object was will be released
to the operating system by the memory allocator. (A segmentation fault
happens if a process tries to access memory that it didn't get from the OS.)

  It's still undefined behavior, though, so anything could happen.

  Actually in most systems it wouldn't. Just because you delete an object
doesn't mean that the memory block where the object was will be released
to the operating system by the memory allocator. (A segmentation fault
happens if a process tries to access memory that it didn't get from the OS.)

  It's still undefined behavior, though, so anything could happen.

Deleting an object does not mean releasing the allocated block...???
can you please explain this, and if not on deleting, then when does
the allocated block be released...??
 
K

Kai-Uwe Bux

Sachin said:
Deleting an object does not mean releasing the allocated block...???
can you please explain this, and if not on deleting, then when does
the allocated block be released...??

Note the operative words "released _to_the_operating_system_". Calling
delete surely "releases" the memory, but only in the sense that subsequence
calls to new might use that memory. Whether the OS gets that memory back is
not specified by the standard. In fact, the standard is well-advised to take
the stance that the OS might be lacking system calls for returning memory.


Best

Kai-Uwe Bux
 
Ö

Öö Tiib

Deleting an object does not mean releasing the allocated block...???
can you please explain this, and if not on deleting, then when does
the allocated block be released...??

Deleting an object does mean it is destroyed and storage of it is
released for your code.

Attempts to access it after deletion are undefined behavior. Undefined
behavior means that it is not defined what happens. There are no
guarantees that it works nor are there guarantees that you get access
violation errors or something. It is your responsibility to not do it.
Computer does not need to waste its resources to check if you are
stupid, it assumes you are not since you are programmer.
 
S

Sachin Midha

Deleting an object does mean it is destroyed and storage of it is
released for your code.

Attempts to access it after deletion are undefined behavior. Undefined
behavior means that it is not defined what happens. There are no
guarantees that it works nor are there guarantees that you get access
violation errors or something. It is your responsibility to not do it.
Computer does not need to waste its resources to check if you are
stupid, it assumes you are not since you are programmer.

Ok...thanks a lot for this.

This means that the code would execute but the behavior is undefined,
this means whatever modifications we do in the code after delete(this)
would occur..e.g. if I modify the value of any global variable, they
would be updated. Right?

Thanks.
 
B

Bo Persson

Sachin said:
Ok...thanks a lot for this.

This means that the code would execute but the behavior is
undefined,
this means whatever modifications we do in the code after
delete(this)
would occur..e.g. if I modify the value of any global variable, they
would be updated. Right?

No, we don't know what would happen. Undefined behavior means that
ANYTHING can happen, including a crash, the program continuing, or
random effects each time.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top