Thread: a simple delete function

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    Smile a simple delete function

    Code:
    #include <iostream>
    using namespace std;
    
    template <typename M>
    void my_delete(M *&p)
    {
    	if (p) delete p;
    	p = 0;
    }
    
    int main()
    {
    	int* ptr;
    	cout << hex << ptr << endl;
    	ptr = new int;
    	cout << hex << ptr << endl;
    	my_delete(ptr);
    	cout << hex << ptr << endl;
    	return 0;
    }
    The code above gives following error under MSVS .Net 2003:
    test4.cpp(17) : error C2664: 'my_delete' : cannot convert parameter 1 from 'int *' to 'void *& '
    A reference that is not to 'const' cannot be bound to a non-lvalue
    Last edited by manav; 12-28-2007 at 06:24 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Regardless of why or why it does not work, this is very unsafe. Remake your delete function using templates. Calling delete on a void pointer is undefined.
    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.

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks Elysia!
    It's working fine and much better now

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, you shouldn't need to check the value of p before deleting it. Calling delete on a NULL pointer should have no effect.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Though you can optimize it slightly by only calling delete and setting the pointer to NULL if p != NULL! Not that it really matters, however.
    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.

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks Elysia! Thanks cpjust!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Though you can optimize it slightly by only calling delete and setting the pointer to NULL if p != NULL!

    Wouldn't a proper implementation of delete already do that for you?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The "optimization" if you can call it that saves time by calling delete in the first place and setting the pointer to NULL.
    At least in some implementations, the C runtime and the C++ one is quite big with lots of code to do a single thing. Just look at new/delete with Microsoft's implementation.
    Think of how many thousands of instructions you'll save from not calling it at all (not to mention you save a function call).

    No, it's not much of an optimization at all since it will be lightning fast anyway.
    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
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You might also consider how often your code will attempt to delete a NULL pointer? If you think it will be a highly unlikely occurance, then removing the if statement will save a few clock ticks/call.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    More likely, it will save time since it will save the time it takes to call and execute delete plus save 0 to the pointer.
    The more often it's called, the more it would save. Theoretically anyway.
    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.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The check for null inside the if has a cost. If the pointer is rarely null, then that cost will outweigh any potential benefit from skipping the code inside the block.

    In addition, my question was whether the delete implementation checks for null itself. If it does (I don't know the implementation, it was an honest question), then that duplicate check means that only the assignment in the deleter function is saved. So again, the pointer would have to be null often enough that the cost of the extra comparison was less than the benefit of the skipped assignment.

    I agree with you that it probably doesn't matter, but I would prefer to do it without the check for null.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Daved View Post
    In addition, my question was whether the delete implementation checks for null itself.
    The delete operator is required by the standard to have no effect if invoked on a NULL pointer. That would imply the implementation of that operator requires a check.

    In practice, some very old C implementations (significantly predating the 1989/90 C standard) would fall over as a side effect of free((void *)NULL) and some early C++ implementations (predating the 1998 standard by at least a decade) would fall over with delete NULL because they were implemented using those older C compilers/libraries. You will probably never use those implementations unless you beg/borrow vendor-specific compilers from museums.

  13. #13
    Banned
    Join Date
    Nov 2007
    Posts
    678
    I just wanted a delete that would set the pointer to NULL after freeing the memory.
    Standard delete does not do this:
    Code:
    // MSVS .Net 2003
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int* p;
    	cout << "Grabage: " << p << endl;
    	p = new int(0);
    	cout << "After new: " << p << endl;
    	delete p;
    	cout << "After delete: " << p << endl;
    	return 0;
    }

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    How often do you have pointers that require this?

    BTW, here is Stroustrup's comments on the subject, including his implementation of such a function:

    http://www.research.att.com/~bs/bs_f...ml#delete-zero

  15. #15
    Banned
    Join Date
    Nov 2007
    Posts
    678
    No it's not that, if this is very often or not?
    I just wanted the pointers, that don't point to valid memory, to become NULL.
    And the function I made up should be good enough for me. Although after the comments that
    I received here I think I better remove that if statement!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM