Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 deleting pointers
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hamid1455




PostPosted: Sun Feb 17, 2013 9:24 pm   Post subject: deleting pointers

I am learning about pointers in c++ and am confused as to why I receive a debug error when I try to delete a pointer that is storing the address of another variable.

unsigned short int myAge = 5;

unsigned short int * pAge = new unsigned short int;

pAge = &myAge;


delete pAge;

Since I used new, new memory is allocated for pointer pAge, right? Then when I set the memory address using &myAge to pAge, the memory that was allocated previously is still allocated right?

So then why does my last line fail? :cry
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Sun Feb 17, 2013 10:07 pm   Post subject: Re: deleting pointers

The last line fails because the memory to which pAge points to was not allocated by new.

A pointer is nothing more than an integer storing an address in memory. When you call new, you ask the computer to allocate a block of memory large enough for some object (an integer in your case) and return a pointer to this block of memory (basically return it's address). You can then store this address in a pointer to keep track of your block of memory.

However, you can also have a pointer "point" (store the address of) any block of memory you want, in particular, you can store the address of the block of memory for the variable myAge.

Of course assigning a value to a pointer overwrites the old value just like any other variable assignment. This means that pAge does not point to the block of memory you allocated with new, but to the block of memory the program allocated for your variable pAge.

Delete only work for pointers pointing to a block of memory that was previously allocated by new. Hence when you call delete in your last line you get an error since pAge points to the address of myAge which was not allocated by new.
hamid1455




PostPosted: Sun Feb 17, 2013 11:28 pm   Post subject: RE:deleting pointers

so if I assign the pointer a new value, does the block of memory get lost? as in a memory leak?
Insectoid




PostPosted: Mon Feb 18, 2013 9:18 am   Post subject: RE:deleting pointers

If you point the pointer at something else without deleting it you lose that block of memory, only if that block of memory was dynamically assigned. For example:


code:

int foo;
int bar;
int *pfoo;
pfoo = &foo;
pfoo = &bar;//This is not a memory leak, because foo is still accessible from, well, calling foo like a normal int.



code:

int foo;
int *pfoo = new int;
*pfoo = 5 //the memory at *pfoo contains 5
pfoo = &foo; //this is a memory leak, because the memory containing 5 cannot be accessed from anywhere.


code:

int foo;
int *pfoo = new int;
*pfoo = 5
delete pfoo;
pfoo = &foo; //this is not a memory leak, because the memory containing 5 was deleted before the pointer was moved.
hamid1455




PostPosted: Mon Feb 18, 2013 1:06 pm   Post subject: RE:deleting pointers

okay, I understand. So as long as I DO delete an pointer that was initialized previously, there will be no memory leaks.
Insectoid




PostPosted: Mon Feb 18, 2013 1:09 pm   Post subject: RE:deleting pointers

Only delete pointers that point to dynamically assigned memory.

In fact, don't even think of it as deleting pointers, because you aren't. You're deleting the allocation of memory it points at. Since you can't delete statically assigned memory, it makes sense that it only works with dynamic memory allocation.
hamid1455




PostPosted: Mon Feb 18, 2013 2:34 pm   Post subject: RE:deleting pointers

Alright. I'm learning c++ from a book
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: