Thread: about C malloc and free

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    17

    about C malloc and free

    below is the code
    Code:
    #include <stdio.h>
    
    
    typedef struct _test{
        int a;
        int b;
        char data[0];
    }Test;
    
    
    
    
    /****************************************************************************************
      * this eample proved that the data part is not deleted while free!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      **/
    int main(int argc, char *argv[]){
        Test *tt = malloc(sizeof(Test) + 8);
        tt->a = 20;
        tt->b = 20;
        tt->data[4] = ',';
        printf("before free, tt->a is %d, tt->b is %d, tt->data[4] is %c\n", tt->a, tt->b, tt->data[4]);
        free(tt);
        printf("after free, tt->a is %d, tt->b is %d, tt->data[4] is %c\n", tt->a, tt->b, tt->data[4]);
        //free(tt->data);
        //printf("after free the data part, tt->a is %d, tt->b is %d, tt->data[4] is %c\n", tt->a, tt->b, tt->data[4]);
    }
    and the display
    Code:
    before free, tt->a is 20, tt->b is 20, tt->data[4] is ,
    after free, tt->a is 0, tt->b is 0, tt->data[4] is ,
    so the data[] is not freed? how can I delete it ?

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    It is freed. Free simply tells your OS that it is now OK to store other data at those memory locations, it doesn't necessarily actually modify the data that's already there. If you refer to those memory locations later on, the data may still be there.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    The memory block is freed.

    Freeing a block does not necessarily wipe the contents, it only frees the block. If you want to wipe the contents, memset it before freeing it.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And what you're doing by accessing a pointer after it's been freed is undefined behavior anyway. You're lucky it didn't crash the program.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    but the tt->a tt->b is set to be zero, the data part stays the same?. Now I understand that some time later this memory is not usable. but the cleaning .
    seems it only clean the struct _test part?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc vs free()
    By Flames in forum C Programming
    Replies: 24
    Last Post: 03-09-2012, 10:00 PM
  2. malloc() and free()
    By rlesko in forum C Programming
    Replies: 5
    Last Post: 12-05-2010, 01:49 PM
  3. malloc() and free()
    By someprogr in forum C Programming
    Replies: 1
    Last Post: 12-28-2007, 07:16 PM
  4. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  5. malloc(); and free();
    By Perica in forum C Programming
    Replies: 1
    Last Post: 12-20-2002, 05:55 AM