Pointers again!!!

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>
using namespace std;
void main()
{
int i =5;             // this variable is _not_ on the heap

int * ptr = new int;  // 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?
Last edited on
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..

Please correct me if i am wrong

Thanks a lot again :)
@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.

You could start by reading this:
http://cplusplus.com/doc/tutorial/pointers.html

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 was told to use a Lib called Boost to provent any type of memory problems.
Now i'm passing it on to you :P

www.boost.org
BTW It does a lot more than just memory.
I was told to use a Lib called Boost to provent any type of memory problems.


Hi Space, I don't think he need to look into this library, considering the fact that it's a basic concept understanding problem.
Thanks a lot for all your replies and guidance.

@Space : Can you please elaborate on what is the Boost library, never heard of any such term.I am eager to learn any new thing that comes in my way :)

Last edited on
http://en.wikipedia.org/wiki/Boost_C%2B%2B_Libraries

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?
Topic archived. No new replies allowed.