Unsolved Application Is Crashed When delete int*
-
Hello Friends And Qt Experts
when i try to delete
int*
that time my application crashed.
follow my example code.int* CorrectError() { int *Temp = new int[100]; return Temp ; } int main() { int *data = CorrectError(); ////// Some Code ////// delete data ; ///// here my application is crashed. printf("Application Is Closed"); }
please help me to solve my problem
thanks is advance. -
delete [] data ; ///// here my application is crashed.
-
-
@Ketan__Patel__0011
According to me: you do not want todelete data
, asdata
was nevernew
ed. It was*data
that wasnew
ed.~~delete [] *data;~~
EDIT
Ignore above, nonsense, I should have tested.I find no problem here at all with your original
delete data
. Like @Christian-Ehrlicher has said below, all ought to be well. Usevalgrind
, and/or check what the stack trace from the "crash" you report says. -
@JonB said in Application Is Crashed When delete int*:
delete [] *data;
if i try
delete[] *data
that time i got this error.error: C2541: 'delete': cannot delete objects that are not pointers
-
You program should not crash - all is fine (except the delete[] - thing but this does not lead to a crash in Linux or Windows nowadays).
Use e.g. valgrind o see what really happens. -
if i am ignore
delete data;
that i am got memory leakage in my application
that's why i am try to delete the data.
-
@Ketan__Patel__0011 As I said you code is fine and it does not crash nor leak memory here when using
delete[]
-
@Ketan__Patel__0011
I am wondering: is it the case that your progem does not crash on the linedelete data
? Rather, after you have done that, you still accessdata
somewhere, but it has beendelete
d?? You do indeed need todelete data
somewhere to prevent memory leak, but not while you are still using it!Whenever you have a "crash", run your code in the debugger and look at the stack trace to see where the "crash" actually emanates from in your code....
-
@Ketan__Patel__0011 said in Application Is Crashed When delete int*:
if i am ignore delete data;
If you are allocation with
new []
you have to delete it withdelete []
, this is C++ standard.
If it doesn't work, then throw away your C++ compiler and use one which is standard conform.This must work:
int *temp = new int[100]; ... delete[] temp;
=> cf. http://www.cplusplus.com/reference/new/operator delete[]/