Thread: free-ing un-malloc-ed memory

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    free-ing un-malloc-ed memory

    Will it cause problems if I call free() on a char* that I never called malloc() for?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Probably. And what do you hope to achieve with that?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    It was a timing issue I was worried about. There was a chance that the way I had structured my main loop, that I might be freeing a variable that might not have been malloced during the iteration.

    I wondered if I'd need to set a flag to make sure.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bladactania View Post
    It was a timing issue I was worried about. There was a chance that the way I had structured my main loop, that I might be freeing a variable that might not have been malloced during the iteration.

    I wondered if I'd need to set a flag to make sure.
    Another way to check this is to set those variable to NULL when they are declared or freed:
    Code:
    char *ptr=NULL;
    [various things may or may not happen]
    if (ptr) { free(ptr); ptr=NULL; }
    Pointers are never set to NULL (either when initialized or freed) unless you do it, but if they are NULL then you know who did it and why (hopefully: they are already freed or unallocated*). This is easier than using a flag.

    *remember that setting an allocated pointer to NULL does not free it, so would be a memory leak...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I seem to remember seeing somewhere that you cannot allocate memory for a pointer that is already set to NULL. Is this true?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    I seem to remember seeing somewhere that you cannot allocate memory for a pointer that is already set to NULL. Is this true?
    Huh? If what you mean by this is:
    Code:
    int *ptr = NULL;
    ptr = malloc(...);
    would be wrong, then you have misunderstood. This code-snippet is perfectly valid and correct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bladactania View Post
    I seem to remember seeing somewhere that you cannot allocate memory for a pointer that is already set to NULL. Is this true?
    Gotta concur with matsp, I do this all the time and so should you!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Instead of NULL, would setting the prt to 0 work?

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bladactania View Post
    Instead of NULL, would setting the prt to 0 work?
    I guess; but that will become an obscurantist habit (because it is very normal to set a pointer to NULL for exactly these reasons).

    Why not just use NULL????
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    I guess; but that will become an obscurantist habit (because it is very normal to set a pointer to NULL for exactly these reasons).
    It is not as obscure as you think: 0 is a null pointer constant.

    Quote Originally Posted by MK27
    Why not just use NULL?
    Personal choice to avoid the use of a macro.
    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

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    NULL and 0 are essentially the same thing - serves the same purpose and behaves the same. Any particular reason you prefer to use 0?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    It is not as obscure as you think: 0 is a null pointer constant.
    Personal choice to avoid the use of a macro.
    NULL being the macro I guess. That's a decent reason.

    And I suppose if I saw this:
    Code:
    char *ptr=0;
    I would immediately associate it with NULL, so not obscure at all. Sorry.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But then if you have a long function with something like this:
    Code:
    char* blah = 0;   // You can clearly see it's a pointer since you see the declaration.
    ...
    ...
    blah = 0;   // Not so obvious 50 or more lines down since you can't see how blah is declared.  Is it a pointer or an int?
    That's why I use NULL. It makes is clear that the variable it's being assigned to is a pointer.
    And yes, since NULL is 0 you could assign NULL to an int, but that would just be dumb.

    Although, usually the name of the variable itself gives you a pretty good idea about it's type since a lot of people prefix pointers with 'p'...
    "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

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    Not so obvious 50 or more lines down since you can't see how blah is declared.
    Hence a function should do one thing and do it well

    Quote Originally Posted by cpjust
    That's why I use NULL. It makes is clear that the variable it's being assigned to is a pointer.
    And yes, since NULL is 0 you could assign NULL to an int, but that would just be dumb.
    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.
    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

  15. #15
    Registered User
    Join Date
    May 2007
    Posts
    147
    Bladactania:

    I seem to remember seeing somewhere that you cannot allocate memory for a pointer that is already set to NULL. Is this true
    The closest I can say that's remotely connected to is realloc

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