Thread: when will we use "free" in c

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    49

    when will we use "free" in c

    Hi All, when I use "free" pointer in C, I got Sigabrt.

    Code:
    char* output=malloc(8*sizeof(char));
    output="abcdefg";
    printf("%s", output);
    
    free(output); //<----SIGABRT error

    Another Question:
    If we want to return a char pointer in a function, and I need to free the pointer, when I free the pointer, will it affect the return value?
    for e.g.
    Code:
    char* tmp()
    {
      char* output=malloc(8*sizeof(char));
      output="abcdefg";
      printf("%s", output);
    
      return output;
      free(output);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well your first one was right, until you did this

    > output="abcdefg";

    So when you called free, it wasn't with the pointer returned by malloc.

    Try using
    strcpy(output,"abcdefg");
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    output="abcdefg";
    That's not how you get a string into your allocated memory; you use strcpy. What you're doing there is allocating the memory, then abandoning it (leaking it) and changing the pointer to point to a string literal in read-only memory, which causes the fault when you try to free it (because it's read only).

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by homoon View Post
    Another Question:
    If we want to return a char pointer in a function, and I need to free the pointer, when I free the pointer, will it affect the return value?
    for e.g.
    Code:
    char* tmp()
    {
      char* output=malloc(8*sizeof(char));
      output="abcdefg";
      printf("%s", output);
    
      return output;
      free(output);
    }
    That free will have no effect because it is after the return, so it is unreachable. If you had this:

    Code:
    char* tmp()
    {
      char* output=malloc(8*sizeof(char));
      strcpy(output, "abcdefg");
      printf("%s", output);
    
      free(output);
      return output;
    }
    Do not do this, it will cause problems. When you malloc, you get a chunk of memory that is "yours" until you free it. When you free it, the memory is considered available for allocation again.
    This code snippet can blow up in your face, because free() does not change the value of the pointer. The pointer will still point to the string you copied in, even after you return. However that memory is now considered free and can be reused by a subsequent malloc. So it is wrong to do this but it probably won't fail straight away.

    You should free() when you are finished with the pointer. The calling function can free it when it is not using it anymore.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    thx all, you all solved my problem, many thanks!!!

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    I am a little late in the discussion but here is my understanding.

    Once you use malloc(), you create a memory block that you must manage... or free. So when to use free()?

    Within a function, you want to free() any memory that you will not use outside of the function. This will prevent memory leaks.

    If you want to access the memory outside the function, you have to return a pointer to the memory, since you cannot return memory from a function.

    BTW, I'm still learning, so I could be wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *tmp ()
    {
      char *output = malloc (8 * sizeof (char));
      strcpy (output, "abcdefg");
      printf ("In  tmp(), char *output.\tAddress: %p\tContains: %s\n", output, output);
      /* free(output); Using free here, would basically be saying: I no longer need the memory which contains "abcdefg".  */
      return output;
    }
    
    int main ()
    {
        char *string = tmp (); /* The pointer *string will point to the memory created with malloc() inside the tmp() function. */
        printf ("In main(), char *string.\tAddress: %p\tContains: %s\n", string, string); /* Access the memory here. */
        free (string); /* free() here since we are done with string. free() will free memory that was created with malloc() inside the tmp() function. */
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to "free" an array cell.
    By skytreader in forum C Programming
    Replies: 3
    Last Post: 02-18-2010, 06:07 AM
  2. Replies: 5
    Last Post: 01-18-2010, 07:25 AM
  3. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM