Thread: delete pointers

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    delete pointers

    Hi, my question is what is wrong with this code?
    Code:
    class T;
    
    int main(){
    
    T *p = 0;
    delete p;
    
    }
    I do not think if there is anything wrong here but the expert who posed this question thinks that there is definitely something conceptually wrong here?

    The only things that I can think of are:

    if = or delete are overriden

    does an overriden p matter here?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I can't see anything wrong with it.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're deleting a pointer to a incomplete type. That's undefined behaviour.

    Given that the pointer is null, nothing will actually happen, but that doesn't change the fact that the program is invalid, and a compliant compiler might simply refuse to compile it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That should at least generate a warning.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    That should at least generate a warning.
    Only if you turn on some warnings. Too many people don't bother compiling with Warning Level 4 in MSVC++ or -Wall in gcc.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If the type (class T) isn't complete, it will not have the destructor or overridden operators called on the object [although, technically, it is probably not at all defined what happens in this case - but most compilers will just treat it as a non-class destruction, just like if you use new + delete to allocate and destroy a plain data struct].

    Actually, assigning a pointer of a class doesn't allow for operator overrides, since you can't actually override based on "pointer to type X is assigned a value" - you can override "X is assigned a value" [assuming X isn't a basic type].

    Whether delete is overridden or not, it should cope with a 0 delete without doing anything wrong, and it shouldn't make any difference to this code. Delete of "0" is a valid case and should be allowed to pass through without doing anything.

    --
    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
    Apr 2006
    Posts
    2,149
    "Conceptually wrong" doesn't mean the code will generate an error. It means "WTF were you thinking writing that code?". Why would you call delete on a pointer to an object that you haven't defined completely? Or that you know to be null?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by King Mir View Post
    "Conceptually wrong" doesn't mean the code will generate an error. It means "WTF were you thinking writing that code?". Why would you call delete on a pointer to an object that you haven't defined completely? Or that you know to be null?
    True indeed. But then most examples provided in class, at job interviews, discussion forums, etc, are usually "distilled down" so that the code is very specifically doing what it needs to show a concept, but nothing beyond that. I take the above code as such - no, it's not something you would ever actually write, but throw in a few if's, for/while-loops and some "real code" in the middle - where some of those conditions may mean that the code passes all the way through without modifying p, and we have a real world scenario where this would happen.

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

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By the way, I'm wondering if the original poster's "expert" is confusing delete with free - it is NOT valid to call free with a NULL value - it will attempt to free address zero, with usually immediate dire consequences.

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

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by matsp View Post
    By the way, I'm wondering if the original poster's "expert" is confusing delete with free - it is NOT valid to call free with a NULL value - it will attempt to free address zero, with usually immediate dire consequences.
    It is perfectly valid to call free() with a null pointer, and the result is exactly the same as deleting a null pointer. See the specification of free(), 7.20.3.2/2 in the C99 standard. See also the specification of free() in the Single Unix Specification.

    Maybe it's not valid in C90.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    The single biggest problem I can see is that you are implicitly creating the p object on the stack, and deleting it from the heap.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    It is perfectly valid to call free() with a null pointer, and the result is exactly the same as deleting a null pointer. See the specification of free(), 7.20.3.2/2 in the C99 standard. See also the specification of free() in the Single Unix Specification.

    Maybe it's not valid in C90.
    I don't know if it's simply my memory, but I'm 100% sure I've had problems trying to free a NULL-pointer. It may of course be pre-C90 even, since I generally try not to even get to the free when the pointer is NULL.

    I had a quick look at google "man free" before I posted, the place I found didn't say anything [either direction] about using free with a NULL pointer, so I assumed that this was still a problem.

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

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    The single biggest problem I can see is that you are implicitly creating the p object on the stack, and deleting it from the heap.
    How's the above different from this:
    Code:
    int main(int argc, char **argv[]) 
    {
       int *p = 0;
    
       if (argc > 1) 
          p = new int;
       delete p;
    
       return 0;
    }
    The above code will definitely run and perform correctly. I guarantee it.

    --
    Mats
    Last edited by matsp; 10-29-2007 at 10:12 AM. Reason: Fix according to LaserLights comments.
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The above code will definitely run and perform correctly. I guarantee it.
    I think that the C++ Standard guarantees it too, but only if you actually declare the command line arguments or otherwise declare argc. If not, argc does not exist, so you make a poor guarantor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    It is perfectly valid to call free() with a null pointer, and the result is exactly the same as deleting a null pointer. See the specification of free(), 7.20.3.2/2 in the C99 standard. See also the specification of free() in the Single Unix Specification.
    Perfectly valid in the spec maybe, but this is one of those cases where you can't really rely on the spec. Using C++ delete on a null pointer is safe, but calling C free() on a NULL pointer crashes on about half the machines I have access to, specifications notwithstanding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete functor
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2009, 08:25 AM
  2. why delete[] and delete?
    By dude543 in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2005, 11:55 PM
  3. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM