Thread: Free function not working

  1. #1
    Registered User
    Join Date
    Mar 2022
    Posts
    1

    Free function not working

    I'm trying to free a char pointer, and I see the below behaviour

    Case 1:
    Code:
    int main()
    {
    char *oneLineTerm;
    free(oneLineTerm);
    printf("freed memory"); /* Not printed */
    }
    Case 2:
    Code:
    int main()
    {
    char *oneLineTerm;
    if(*oneLineTerm != NULL)
        free(oneLineTerm);
    printf("freed memory"); /* Printed */
    }
    What exactly happening in free() function here?

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Shouldn't you be using a newline or flushing the stdout buffer here:
    Code:
    printf("freed memory");
    i.e:
    Code:
    printf("freed memory\n");//added newline
    Also... What's the value of the pointer you are free'ing? Hint! Stack variables are not bitwised zeroed.
    Last edited by G4143; 03-15-2022 at 04:10 AM.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by aravindsreeram View Post
    I'm trying to free a char pointer, and I see the below behaviour

    Case 1:
    Code:
    int main()
    {
    char *oneLineTerm;
    free(oneLineTerm);
    printf("freed memory"); /* Not printed */
    }
    Case 2:
    Code:
    int main()
    {
    char *oneLineTerm;
    if(*oneLineTerm != NULL)
        free(oneLineTerm);
    printf("freed memory"); /* Printed */
    }
    What exactly happening in free() function here?
    free() is ONLY used if memory has been allocated using malloc(), calloc(), or realloc()! Or some other function that returns a pointer to memory it allocated. Always read the man pages for functions to understand the function completely!

    From "man 3 free" on my Linux system:
    The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc(), or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.
    Please study my version of your code for a proper use of free():
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DIM 100
    
    int main()
    {
       char *oneLineTerm = NULL;  // ALL local variables MUST be initialized
    
       oneLineTerm = malloc(DIM);
    
       if(oneLineTerm == NULL)  // Unknown error allocating the memory
       {
          printf("ERROR: Memory not allocated, Nothing to see here, move along, move along!\n");
          return EXIT_FAILURE;
       }
    
       // Memory has been allocated
       // More code here using the pointer
    
       free(oneLineTerm);  // NOW, free the allocated memory
       oneLineTerm = NULL; // NULL the pointer in case some other code is inserted below before exiting!
    
       printf("freed memory\n");
    
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    You should also mention that the C compiler has plenty of flags that can set to detect many, many, many errors. Here's an example of some of the more common ones.
    Code:
    gcc -Wall -pedantic -Werror ...

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by G4143 View Post
    You should also mention that the C compiler has plenty of flags that can set to detect many, many, many errors. Here's an example of some of the more common ones.
    Code:
    gcc -Wall -pedantic -Werror ...
    I could not make any assumption as to which O/S, compiler, and possible IDE the OP was using, but yes, no matter what compiler a programmer is using, warnings should be turned on and turned up high to trap all warnings and errors, for coding any program. That applies to any programmer, beginner, intermediate, or expert.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rstanley View Post
    free() is ONLY used if memory has been allocated using malloc(), calloc(), or realloc()! Or some other function that returns a pointer to memory it allocated. Always read the man pages for functions to understand the function completely!
    AND... free works (do nothing) if the pointer is NULL (ISO 9899 7.20.3.2 § 2)... But if the pointer isn't NULL and not allocated by the functions above, then the behavior is unspecified (probably a segmentation fault or access violation).

    This is OK:
    Code:
    void *p = NULL;
    
    free( p );  // ok, do nothing
    But this:
    Code:
    int main( void )
    {
      int *p;
    
      free( p ); // probably will crash
    }
    Because `p` isn't initialized and can have ANY (probably invalid) value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-14-2014, 08:28 AM
  2. Free Lancer Taxes: Working for foreign country
    By AmazingTrans in forum General Discussions
    Replies: 2
    Last Post: 11-22-2013, 01:34 AM
  3. if function not working (its inside anot if function)
    By ahahsiol in forum C Programming
    Replies: 2
    Last Post: 03-08-2013, 08:55 AM
  4. free() not working?
    By scout in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 12:25 PM
  5. free not working with malloc
    By P4R4N01D in forum Windows Programming
    Replies: 10
    Last Post: 12-19-2007, 09:08 PM

Tags for this Thread