delete pointers

This is a discussion on delete pointers within the C++ Programming forums, part of the General Programming Boards category; Hi, my question is what is wrong with this code? Code: class T; int main(){ T *p = 0; delete ...

  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,893
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    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,050
    "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,893
    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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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
    21,910
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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.

Page 1 of 2 12 LastLast
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, 10: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, 12:18 PM

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