Thread: Deleting NULL.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Deleting NULL.

    While on Windows I had no problems, I was wondering if it's safe to delete a variable that's already NULL by standard. I want to make sure my code is portable without the need to add an if check every time I want to delete.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If deleting NULL is part of standard behaviour, then go ahead and do it.

    If it doesn't work, then it's down to that specific version of your compiler rather than anything to do with your OS. Another compiler for the same OS would likely do the right thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The standard defines deleting a null pointer to be a noop. VC++6 didn't follow the standard there and created an access violation, but except for that one outdated compiler, I think pretty much all of them follow the standard.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM