freeing a NULL pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manu1001
    New Member
    • Jul 2007
    • 14

    freeing a NULL pointer

    can i free a NULL pointer?
    is free(NULL) guaranteed not to cause trouble in ansi c?
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by manu1001
    can i free a NULL pointer?
    is free(NULL) guaranteed not to cause trouble in ansi c?
    No,you must check to see whether pointer is NULL before deleting it,or your program will crash.

    Comment

    • manu1001
      New Member
      • Jul 2007
      • 14

      #3
      but, wouldn't it be so much better if free() could check for NULL and simply ignore those cases?
      instead the user will have to to do the check now and it wouldn't help the readability if there are such checks all over the program.

      if (ptr1 != NULL)
      free(ptr1);
      if (ptr2 != NULL)
      free(ptr2);
      ...
      and so on.

      is there any particular reason for ansi not having this feature?

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        Originally posted by manu1001
        can i free a NULL pointer?
        is free(NULL) guaranteed not to cause trouble in ansi c?
        You can free a NULL pointer. Nothing will happen (as defined by the standard).

        Comment

        • hsn
          New Member
          • Sep 2007
          • 237

          #5
          people
          will it crash or not.
          i am not a user of c but i really want to learn about it.
          will it crash or not???????
          and why????????????

          regards
          hsn

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            will it crash or not.
            No, it won't. Freeing a null pointer is defined behavior. It's defined as doing nothing.

            and why????????????
            Because the standard says so.

            And hsn, format your posts properly. Poor capitalization, poorly formatted posts, excessive question marks, etc. don't sit well with people. This isn't IM chat. You have the time to properly write your posts. Do so, and you'll get more useful answers.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Just in case someone doesn't believe the previous poster, here is the text from
              the final draft of the C99 Standard:

              Originally posted by C Standard
              7.20.3.2 The free function

              Synopsis

              [#1]

              #include <stdlib.h>
              void free(void *ptr);

              Description

              [#2] The free function causes the space pointed to by ptr to
              be deallocated, that is, made available for further
              allocation. If ptr is a null pointer, no action occurs.
              Otherwise, if the argument does not match a pointer earlier
              returned by the calloc, malloc, or realloc function, or if
              the space has been deallocated by a call to free or realloc,
              the behavior is undefined.

              Returns

              [#3] The free function returns no value.
              kind regards,

              Jos

              Comment

              Working...