Hi
I just get a simple problem:
the app crashed when I ran it, why?Code:#include <iostream.h> void main() { int i = 2; int* ptest = &i; delete ptest; }
Thanks
Hi
I just get a simple problem:
the app crashed when I ran it, why?Code:#include <iostream.h> void main() { int i = 2; int* ptest = &i; delete ptest; }
Thanks
Don't laugh at me,I am just a SuperNewbie.
You are deleting memory you haven't allocated with new.
int main(), not void main().
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
> delete ptest;
If you have
int *ptest = new int;
Then you need at some point, exactly one
delete ptest;
If you have
int *ptest = new int[some_number];
Then you need at some point, exactly one
delete [] ptest;
Trying to delete anything else is a really bad idea.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.