Thread: Simple question about free()

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    27

    Simple question about free()

    If I call free(NULL) or free(ptr) where ptr == NULL, will it crash the program or not compile?

    I'd try it out myself, except there isn't really an easy place in my existing code to do it and I'd have to go through a whole bunch of overhead stuff to create a new project in the environment I'm working on. Thanks for any help.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    sorry for this thread, after more looking around, I found out free(NULL) would indeed crash the program. Thanks.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by fanoliv
    sorry for this thread, after more looking around, I found out free(NULL) would indeed crash the program.
    From what unfortunate resource did you get that?

    free(NULL) is well defined: "no action occurs".
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    Really? Well, I was just searching through these forums actually. Looks like I'll have to be more careful next time. Thanks a lot for your reply.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The *NIX manual pages are a great resource for C functions. If you're not in a *NIX environment, you can find them here:
    http://www.die.net/doc/linux/man/

    The one for free() specifically is: http://www.die.net/doc/linux/man/man3/free.3.html

    And on that page you'll find:
    The free() function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation. If ptr is a null pointer, no action shall occur. Otherwise, if the argument does not match a pointer earlier returned by the calloc(), malloc(), posix_memalign(), realloc(), or strdup() function, or if the space has been deallocated by a call to free() or realloc(), the behavior is undefined.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Dave_Sinkula
    From what unfortunate resource did you get that?
    Quote Originally Posted by fanoliv
    Really? Well, I was just searching through these forums actually.
    D'oh!

    Actually, I'm curious where such a thing was said. Do you know what text you used for a search -- or better, can you point out the thread(s)?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    27

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    fanoliv, check out the last post in that thread -- the one by jafet.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM