Hi all,
I have a small doubt . I have written a code to check how allocation in memory is done. Now when i am deleting the pointer , it is crasing. Below is my code
# include <iostream>
using namespace std;
void main()
{
int i =5;
int * ptr = new int;
ptr =&i; delete ptr;
cout<<"after del";
cout<<ptr;
getchar();
}
My program is crashing when i am doing delete ptr;
My query is why is this happening. I am allocating memory to the ptr using new and then i am making it store the memory address of i. Where am i wrong or is there a silly error somewhere. Please help.I am getting heap corruption error on Visual Studio
(Please use the "#" Button on the right to enclose your code in the right brackets.)
You can only "delete" pointers pointing to positions on the heap (i.e. "new"d variables).
The operator delete really means "delete this thing i point to" not "delete me (= the pointer)".
After a "delete ptr" you can still use the ptr (point to something else) but not the thing the pointer pointed to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# include <iostream>
usingnamespace std;
void main()
{
int i =5; // this variable is _not_ on the heap
int * ptr = newint; // now ptr points to a location on the heap
ptr =&i; // now ptr points to a location not on the heap
// you also lost the last reference to the int on line 5 7 => memory leak
delete ptr; // you try to delete a location not on the heap => bad idea
cout<<"after del";
cout<<ptr;
getchar();
}
Maybe you should read some book/tutorial on pointers?
Thanks a lot for the reply. It is much clearer now.
Also i think the memory leak is at line 7 where i am allocating some memory in heap and then not at all using it and its reference is lost..
@r2m
There is a memory leak problem in your program. That is not causing your program to crash. You allocated an integer via the heap. Then you reassigned the pointer to point to the address of the stack variable i. At this point you have a memory leak because you can no longer recover the address of the integer you allocated with operator new. As Onur pointed out you are trying to delete memory that you didn't create on the heap. This is unacceptable.
Look out for some tutorials on memory allocation and read those as well. There may be some nice articles on this site if you search through the database of articles.
I haven't used Boost a whole lot, but I don't remember there being much related to memory management, and I can guarantee that using Boost won't prevent against all kinds of memory errors.
Actually it is to understand the basic concepts of pointers and explicit memory mamagement. But later, one might come to more complex "object networks" where it is not easy to know who is responsible for object destruction. In such cases the boost::smart_ptr can come in handy: http://www.boost.org/doc/libs/1_38_0/libs/smart_ptr/smart_ptr.htm
Now when i am deleting the pointer , it is crasing.
what does crashing exactly mean here. Is it that the program on execution shows a bad address or it stops execution before it should ideally do or something else?