deleting dynamic allocation c and c++

Jun 3, 2013 at 1:56pm
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 Jun 3, 2013 at 2:16pm
Jun 3, 2013 at 2:10pm
Why do not you want to open a book on C and read the description of free yourself?!
Jun 3, 2013 at 2:17pm
tell me any book that has this example
Jun 3, 2013 at 2:24pm
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 Jun 3, 2013 at 2:25pm
Jun 3, 2013 at 3:00pm
what can i do.


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