delete a pointer with unallocated memory

This is a discussion on delete a pointer with unallocated memory within the C++ Programming forums, part of the General Programming Boards category; Hi, I learned that deleting memory that has not been allocated will cause segment fault. e.g. Code: int main () ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    delete a pointer with unallocated memory

    Hi,
    I learned that deleting memory that has not been allocated will cause segment fault. e.g.
    Code:
    int main ()
    {
      int * p;
      delete p;
    }
    .
    However, I have seen in someone's code that
    Code:
    int main ()
    {
      int * p = 0;
      delete p;
    }
    It seems work fine.
    Is the second one legal? Does this mean that the memory at address 0 is somehow special?
    How to check if a memory has been allocated before deleting it?
    Thanks and regards!
    Last edited by lehe; 04-20-2009 at 06:22 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you haven't allocated memory, then don't try to free it. If you have code where you "don't know" if it's allocated or not, then you do indeed need to make sure you have it set to 0 (aka NULL) when it's not allocated.

    You do not delete the pointer, you delete the memory that it is pointing to. If it's not pointing to memory that you got through new, then it should not be deleted.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It means that delete only deletes the pointer if it's != 0.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Deleting a NULL pointer (aka a pointer with a value of 0) is perfectly legal (as defined by the standard). Deleting anything non-allocated is undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    147
    From OP:

    How to check if a memory has been allocated before deleting it?


    First, the only way to 'check' is to insist that all unallocated pointers are initialized to 0, which then answers your question - all non-zero pointers would refer to allocated space.

    However, this doesn't address the potential that a pointer that once identified a valid memory location has not been 'deleted' by some other code, where the pointer in question has not been set to zero after the delete.

    This problem is one reason that many modern C++ developers use smart pointers, especially reference counted smart pointers. In trivial code it may seem unimportant and not worth any performance penalty, but as the ambition of the target application increases, the complexity of designs, especially in the presence of threaded designs, virtually demands the use of smart pointers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JVene View Post
    ...especially in the presence of threaded designs...
    I don't disagree with the purpose/use of smart pointers, but I fail to see what threading has to do with the matter?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I don't disagree with the purpose/use of smart pointers, but I fail to see what threading has to do with the matter?
    If you are sharing objects among threads, reference counting is the easiest way to ensure that the object lifetime is managed correctly. I believe that was his point in regards to multi-threaded applications (granted it has little to do with the original post).

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    147
    bithub has my point correct, exactly.

    ..and yes, I'm drifting - sorry. I think it is a tributary of the subject that eventually does apply on the general question of containment, the subject from which the OP's question derives.

    Perhaps I'm too far beyond the scope of the topic, though.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Quote Originally Posted by lehe View Post
    Is the second one legal? Does this mean that the memory at address 0 is somehow special?
    How to check if a memory has been allocated before deleting it?
    Thanks and regards!
    Yes, it's completely legal and normal. 0 (or commonly known as NULL) is a special address effectively meaning "not allocated".
    You don't check if memory has been allocated before deleting it. You simply start out with the pointer set to NULL and if nothing changes that pointer then it is totally safe and normal to just call delete on it regardless. You don't have to check for NULL because they were kind enough to make that check built into delete.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Pointers" <-- crappy name
    By argv in forum General Discussions
    Replies: 64
    Last Post: 06-25-2009, 08:18 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21