deleting dynamic allocation c and c++

i thought that "delete []" is used only for arrays so is the following code correctly done?
1
2
3
     int* pi = new int;
     delete [] pi;
     delete pi; 

Does the first delete do nothing and the second one works? Can someone explain if this codes syntax is correct or not.what can i do.
Last edited on
Why do not you want to open a book on C and read the description of free yourself?!
tell me any book that has this example
Does the first delete do nothing and the second one works?

The code is wrong. Remove delete [] pi;.

See these for more information:
http://www.parashift.com/c++-faq/freestore-mgmt.html
http://www.parashift.com/c++-faq/double-delete-disaster.html

Conclusion: do not delete more times in a row, because you will be introducing a silent bug, which may cause a crash when the program is run. And also, always pair new with delete and new[] with delete[], for the same reason.
Last edited on
what can i do.


Did you even try stepping through it??
Topic archived. No new replies allowed.