weird delete construction

This is a discussion on weird delete construction within the C++ Programming forums, part of the General Programming Boards category; Do I have a memory leak here or not? Code: int *x = new int; delete x, x = 0; ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    weird delete construction

    Do I have a memory leak here or not?

    Code:
    int *x = new int;
    
    delete x, x = 0;
    I saw this in real code, but I'm not sure of the comma operator here.
    I know its weird code and shouldnt be in production code but I'd still like to know

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's valid, and yes, it's weird.
    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

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So it deletes x and then assigns x to 0?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    So it deletes x and then assigns x to 0?
    Yes, and if I code-reviewed that, I'd suggest splitting it: delete x; x = 0; on a line for each of those statements.

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

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    That actually looks pretty handy. Although I'd take it a step further:
    Code:
    delete x, x = NULL
    Now you can debug double-check for invalid dereferences by putting in an if( x == NULL )throw e;

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    That's why Bjarne recommends that you create a safe_delete function that deletes the pointer and sets it to NULL:
    Code:
    template<typename T> void SafeDelete(T& p)
    {
        delete p;
        p = NULL;
    }
    
    int main()
    {
        int* x = new int;
        SafeDelete(x);
        delete x; // Safe, because x is NULL.
    }
    http://www.research.att.com/~bs/bs_f...ml#delete-zero
    Last edited by Elysia; 10-16-2008 at 07:00 AM.
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by @nthony
    Although I'd take it a step further:
    The net effect is the same, and in fact the code after preprocessing may even be exactly the same since NULL may be defined as 0.

    Quote Originally Posted by Elysia
    That's why Bjarne recommends that you create a safe_delete function that deletes the pointer and sets it to NULL:
    That's a paraphrase though
    But it seems to me that Stroustrup's destroy() function template is a little more descriptive in its signature since it takes T*& instead of a T&, so it is more obvious that it works on pointers.
    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

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Yes, it may be more obvious, but it's also less flexible.
    As long as a custom object has overloaded operator delete and and operator = (int), it can work with that function as well.
    If you're going to make a generic function, you might as well go the whole way.
    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.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    So you're sure that the comma operator doesnt change the line into "delete 0;"
    because it takes the last expression as value?

    I mean, in this expression

    Code:
    while (scanf("%d", &d), d != 0)
    The result of scanf is discarded and only d != 0 gets evaluated.
    Isn't that the same in delete x, x = 0? The x gets discarded and x is set to 0 and that gets passed to delete?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Elysia
    As long as a custom object has overloaded operator delete and and operator = (int), it can work with that function as well.
    hmm... I am not convinced though: what does it mean to use destroy()/SafeDelete() on something other than a raw pointer? Also, wouldn't operator delete invoke the destructor, upon which the use of operator=(int) results in undefined behaviour?
    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

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    You're probably right. I wouldn't want to try it on a non-pointer, but there's no harm in using T& instead of T*&, or perhaps there is?
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by KIBO
    So you're sure that the comma operator doesnt change the line into "delete 0;"
    because it takes the last expression as value?
    As far as I can tell, the comma operator has lower precedence than delete, so we are looking at:
    Code:
    (delete x), (x = 0);
    rather than:
    Code:
    delete (x, x = 0);
    Nonetheless, this potential confusion reinforces matsp's point.

    Quote Originally Posted by Elysia
    I wouldn't want to try it on a non-pointer, but there's no harm in using T& instead of T*&, or perhaps there is?
    I do not think that it is harmful, just a little less descriptive in the function signature.
    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

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    As long as a custom object has overloaded operator delete
    That operator works on pointers to the object, not the object itself.

    What you need is an unambiguous implicit conversion to a pointer type.

    Basically, it would be a bad idea for everything except a smart pointer (since it doesn't make sense to delete anything besides a smart pointer) and it's a bad idea for smart pointers too, because the entire point of smart pointers is that you don't call delete yourself.
    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

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    I would question, if x is deleted, why it remains in scope. Typically a pointer to a dynamically allocated object should go out of scope almost immediately after it is deleted.

    In fact, this binding of deletion with going-out-of-scope is exactly what a smart pointer does. Perhaps you should use one.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Using commas is a rough practice no matter how you slice it. It is not as widely accepted in common practice thus its not too weird to see a programmer dumbfounded when they are introduced into a block of code. Either way I see now huge problem just making something like that an entirely separate line.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 05:24 PM
  2. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  3. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 10:17 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

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