right behaviour ?

i try following code

1
2
3
4
5

int xc = 10;
int * i = &xc;
delete i;


it works with aCC compiler , but is it valid ? how can i delete stack ?
This is bad practice.

the stack space used by xc won't be released - but the destructor will be called by delete.
When the variable xc goes out of scope, the destructor will be called again.
So the destructor is called twice.

This double call of the destructor can be fatal - depending on the object being destructed.
Last edited on
Sorry, deleted my post because I realized there was no use of malloc... why would you want to delete i?

-ceruleus
Last edited on
1'st thanks to you for replying my post.

actually i want to use malloc but miss it , still code work .
(how ? it should throw exception ) i try same in visual studio in windows throws exception there. (as expected )

well only thing i can see is i have no idea (knowledge ? ) how compiler work in above two cases ?

That's just undefined behavior. That means the program can crash, nothing can happen, data can be corrupted or all the contents of your fridge destroyed.
Never do it, that's all you need to remember.
You may only use delete on a pointer that was returned by new.
Topic archived. No new replies allowed.