a simple delete function

This is a discussion on a simple delete function within the C++ Programming forums, part of the General Programming Boards category; I don't believe you have to do anything at all. If the if statement is better or not, we can't ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    I don't believe you have to do anything at all. If the if statement is better or not, we can't really say. The best way is to use a profiler, but that would be a little overkill, wouldn't it?
    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.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Unless you're deleting millions of pointers/second, 1 more or less instruction will be completely unnoticable.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> If the if statement is better or not, we can't really say.
    We can discuss it and come to the best logical conclusion possible. My logic tells me it is better to leave it out. Apparently Stroustrup agrees, so I would take it out. Definitely not a big deal but why not make the best decision possible?

    If you don't care about the optimization, then use the clearer version, which again is the one without the if, IMO.

  4. #19
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Why not just use a macro like
    Code:
    #define SAFE_DELETE(p) { delete p; p = NULL; }
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Because macros do not respect scope, are not type-safe, can be used on a void pointer, etc etc.

    Macros like yours also will also result in a compilation error with code like;
    Code:
    if (some_condition)
       SAFE_DELETE(some_pointer);
    else
       something_else();
    although that's a property of your macro, which can be addressed by;
    Code:
    #define SAFE_DELETE(p) do {delete p; p = NULL;} while (0)

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    I don't like the name SAFE_DELETE. An individual call to SAFE_DELETE is no safer than just calling delete. After all, that's the first thing SAFE_DELETE does anyway.
    I would take a lesson from Delphi which has FreeAndNil, calling this DELETE_AND_NULL, to properly say what it does.

    Then I would proclaim that it is so ugly and pointless that I would throw it away and just use delete instead. I would only NULL the pointer when I actually need to, which is almost never if you properly use RAII.
    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"

Page 2 of 2 FirstFirst 12
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, 06: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

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