Deleting a pointer?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

Re: Deleting a pointer?

 
0
  #1
Jan 4th, 2009
I have a rather simple question, and I' ve been looking for the answer for awhile, but did not find any.
So I would like to know, what is the proper way of deleting a pointer.

For example I have a class with some kind of array, which I want to extend later. But what if, for some reason, I won't have to. Like, I am opening a file, but it is not there, so I don't need to allocate any memory. Eveything is OK, then destructor starts working and then the whole program crashes. Altough It works fine if it had some amount of memory allocated before.
So would you please show me the proper way of deleting pointers?

Thank you very much.

  1. myClass::~myClass()
  2. {
  3. if( my_array != 0 )
  4. {
  5. delete []my_array;
  6. }
  7. }
I didn't want to off, so if it is just kill it. 'SOORY'
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Deleting a pointer?

 
1
  #2
Jan 4th, 2009
Initialized the pointer to 0 if you aren't allocating memory. You can safely delete a null pointer and it will effectively be a no-op.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Deleting a pointer?

 
1
  #3
Jan 4th, 2009
The question should be asked "how do I delete a pointer if I allocate the memory using xxxx?"

Let us review:

if you write
  1. double* Ptr=new double[50]; // Allocate space for 50 double s
  2. // ... other code
  3. delete [] Ptr; // Delete the pointer to 50 doubles
  4. // CARE:
  5. // Ptr is still pointing to a value

but if you write
  1. double* Ptr=new double; // allocate 1 double
  2. //.... other code
  3. delete Ptr;

Note as pointed out, delete removes the memory for the pointer BUT does not change the value of the pointer variable. e.g.
  1. ;
  2. std::cout<<"Ptr memory location == "<<reinterpret_cast<long int>(Ptr)<<std::endl;
  3. delete [] Ptr;
  4. std::cout<<"Ptr memory location == "<<reinterpret_cast<long int>(Ptr)<<std::endl;
  5. //
  6. // THIS CAUSES A RUNTIME ERROR:
  7. // DO NOT DELETE TWICE
  8. //
  9. delete [] Ptr;

Note in the above example , if you set Ptr=0; after the first delete then all is well.

Finally::

  1. double X[50];
  2. //.... code
  3. // NO DELETE Required:
  4. // THIS CAUSES A RUNTIME ERROR:
  5. delete [] X;

If the pointer is from an object that is not allocated via a new then don't delete it.

[Note: that if you are using malloc etc, then you must use free. If you are not using malloc, then don't start using it. ]

Hope this helps
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 25
Reputation: nitu_thakkar has a little shameless behaviour in the past 
Solved Threads: 1
nitu_thakkar's Avatar
nitu_thakkar nitu_thakkar is offline Offline
Light Poster

Re: Deleting a pointer?

 
0
  #4
Jan 4th, 2009
use the free() function for delete the space occupie by pointer


syntax :

free(pointer name)
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 57
Reputation: BevoX is on a distinguished road 
Solved Threads: 12
BevoX's Avatar
BevoX BevoX is offline Offline
Junior Poster in Training

Re: Deleting a pointer?

 
0
  #5
Jan 4th, 2009
Now I get it. Thank you all of you!
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC