Possible Duplicate:
Why does the use of ‘new’ cause memory leaks?
I've encountered a small problem like this
int main() {
int i = *new int;
delete &i;
return 0;
}
It compiles ok, but when executing, the shell gives the following :
a.out(38303) malloc: *** error for object 0x7fff5fbff8cc: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap
while
int main() {
int *i = new int;
delete i;
return 0;
}
runs normally as expected.
What bothers me is that, doesn't the first case use the "new" operator to allocate memory? Why does it cause error when delete it?
I've searched on web for several times, but I cannot find a proper explanation. Can anyone tell me why is it wrong? thanks :)