cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
deleting dynamic allocation c and c++
deleting dynamic allocation c and c++
Jun 3, 2013 at 1:56pm UTC
bigapewhat
(7)
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 UTC
Jun 3, 2013 at 2:10pm UTC
vlad from moscow
(6539)
Why do not you want to open a book on C and read the description of free yourself?!
Jun 3, 2013 at 2:17pm UTC
bigapewhat
(7)
tell me any book that has this example
Jun 3, 2013 at 2:24pm UTC
Catfish4
(666)
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 UTC
Jun 3, 2013 at 3:00pm UTC
mutexe
(2372)
what can i do.
Did you even try stepping through it??
Topic archived. No new replies allowed.