Results 1 to 7 of 7

Thread: Deleting the same pointer twice in C++

  1. #1
    Join Date
    Sep 2009
    Posts
    152

    Deleting the same pointer twice in C++

    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 ?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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.

    .

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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++.

  5. #5
    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.........

  6. #6
    Join Date
    Dec 2007
    Posts
    2,291

    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.

  7. #7
    Join Date
    Jun 2010
    Posts
    2

    Re: Deleting the same pointer twice in C++

    Thanks for the info......

Similar Threads

  1. Changing Stylus pointer to normal Pointer in Tablet PC
    By Kelley in forum Portable Devices
    Replies: 3
    Last Post: 21-06-2011, 07:22 PM
  2. Replies: 4
    Last Post: 20-04-2010, 11:56 AM
  3. Can we use pointer in C#?
    By Zoey Mod in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 10:49 AM
  4. Differentiation between void pointer and null pointer
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 12:11 PM
  5. Replies: 1
    Last Post: 24-08-2009, 09:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,665,295.62372 seconds with 17 queries