Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , ,

Sponsored Links


Deleting the same pointer twice in C++

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
Join Date: Sep 2009
Posts: 152
Deleting the same pointer twice in C++

Sponsored Links
I have taken up C++ programming for learning software programming. I have just began learning it from the last month. I am already confused with alot of concepts in it. One such concept is Pointers. Although I am in the process of learning it can anyone detail me on Pointers? and more importantly What happens if we delete the same pointer twice ?

Reply With Quote
  #2  
Old 09-11-2009
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,278
Re: Deleting the same pointer twice in C++

Even I am not that good with C++ programming because I am also in the process of learning it. But I can help you with definition of pointer. A pointer in C++ is said to point to the memory location which is stored in it. In other words, a pointer variable contains the memory address where the actual object can be found. In C++ while defining pointers we have also specify the type of variable to which it is pointing.

.
Reply With Quote
  #3  
Old 09-11-2009
Member
 
Join Date: May 2008
Posts: 2,000
Re: Deleting the same pointer twice in C++

Code:
class xyz
{
.
.
.
};
 

void disp()
 {
   xyz* X = new xyz();
   delete X;    // first delete
   delete X;    // second delete
  .
  .
  .
 }
Consider the above code,
The second delete X is capable of generating unwanted consequences . This includes crashing your program, corrupting your heap, making bizare and unwanted changes to obkects that are already existing on heap, etc. So hence in C++, never Delete a Pointer twice as the pointer is reletad to a memory location directly.
Reply With Quote
  #4  
Old 09-11-2009
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,371
Re: Deleting the same pointer twice in C++

When you delete the pointer, it still points to the same place - the only difference is that the place was marked as "free" in some system-specific way. This memory location is free and now might be allocated for some other data. Deleting it second time might be problematic as it's no longer free. The second call to delete will probably try to mark it as "free" again. Thus with the second delete the memory location on heap that you are deleting is now been used by some othe object data. Never try to do this in C++.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 28-06-2010
Member
 
Join Date: Jun 2010
Posts: 2
Re: Deleting the same pointer twice in C++

hey can u explain me heap concept related to memory management.........
Reply With Quote
  #6  
Old 28-06-2010
EINSTEIN_007's Avatar
Member
 
Join Date: Dec 2007
Posts: 2,133
Re: Deleting the same pointer twice in C++

The heap is reserved for the memory allocation needs of the program. It is an area apart from the program code and the stack. Typical C programs use the functions malloc and free to allocate and deallocate heap memory. The Debug version of MFC provides modified versions of the C++ built-in operators new and delete to allocate and deallocate objects in heap memory.

More information can be found here.
Reply With Quote
  #7  
Old 29-06-2010
Member
 
Join Date: Jun 2010
Posts: 2
Re: Deleting the same pointer twice in C++

Thanks for the info......
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Deleting the same pointer twice in C++"
Thread Thread Starter Forum Replies Last Post
Changing Stylus pointer to normal Pointer in Tablet PC Kelley Portable Devices 3 21-06-2011 07:22 PM
Email Not Deleting Off Server When Deleting From Email Screen On HTC Sense UI Mahatma Portable Devices 4 20-04-2010 11:56 AM
Can we use pointer in C#? Zoey Mod Software Development 5 29-01-2010 09:49 AM
Differentiation between void pointer and null pointer Ram Bharose Software Development 5 18-01-2010 11:11 AM
How can you prevent deleting folders but allow deleting files a.dorst Operating Systems 1 24-08-2009 09:47 PM


All times are GMT +5.5. The time now is 10:58 AM.