Thread: free-ing un-malloc-ed memory

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by JVene View Post
    The closest I can say that's remotely connected to is realloc
    You can call realloc() on a NULL pointer.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bithub View Post
    You can call realloc() on a NULL pointer.
    I agree, of course you can. You have to be able to allocate pointer for NULL pointers, because of the simple concept of the "initialize your variables before you attempt to use them" mantra.

    All a NULL pointer is is a variable whose value has been set to nothing. It's like initializing an int to 0 first.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    Unless a mistake is made for the same reason as you cite NULL being useful. The reader would then be misinformed unless he/she checked, but if he/she checked then the use of NULL would no longer be useful.
    6 in one, half a dozen in the other...
    Both ways have advantages & disadvantages.
    I would assume that if someone did find a discrepancy as you mentioned, they would fix it so the next reader doesn't have to go through the same head scratching motion. In 10 years I've never seen anyone try to assign NULL to a non-pointer. So I would say it's an extremely remote possibility.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    6 in one, half a dozen in the other...
    Both ways have advantages & disadvantages.
    That's right, which is why I think it boils down to personal choice. In C++ we can wait for nullptr, but I am afraid there is no good solution coming up for C.

    Quote Originally Posted by cpjust
    In 10 years I've never seen anyone try to assign NULL to a non-pointer.
    I have done it myself when using some of those crazy programming interfaces with an excessive number of parameters, some of which are pointers while others are not. It is arguably a problem with the interface, but in such cases it does not matter whether the reader sees a NULL or a 0: he/she has to double check anyway.
    Last edited by laserlight; 04-27-2009 at 10:16 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc calloc and free
    By -EquinoX- in forum C Programming
    Replies: 27
    Last Post: 03-26-2009, 10:59 AM
  2. Correct usage of malloc and free for 2D array
    By disruptivetech in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 05:20 AM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  5. Program that displays amount of free memory
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 01-13-2006, 01:27 PM