#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    How do malloc and free work?


    I am interested in learning how to allocate and deallocate memory dynamically in C. I've heard about malloc and free, but I don't understand how they work. I have used the following code:
    Code:
    int *p;
        p = (int *) malloc(sizeof(int));
        *p = 55;
        printf("p: %ld\n", sizeof(p));
        free(p);
        printf("p: %ld", sizeof(p));
    The size of p is always 8 and its value (55) is always there. Why?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,254
    Rep Power
    2222
    p is a pointer. All pointers are the same length in your system, regardless of the datatype they point to. You're running a 64-bit system, I assume.

    Now, if you were to display sizeof(*p), then you should get the size of what p is declared as pointing to. Try it also with char, short, float, double, and a struct.

    When you call malloc, it allocates a block the size of that amount of memory from the heap, a region of memory that the OS sets up for your program to use for dynamic memory allocation. The heap maintains a list of blocks of memory that are available for servicing malloc calls; I've seen this list referred to as the "Free List" at times. When you malloc, that block is taken off the Free List and when you call free that block is returned to the Free List. That block of memory will remain on the Free List until it gets used again to service a malloc call. What you had written to that block of memory will remain there until it gets overwritten, normally as a result of its having been allocated through another malloc call. However, in that case, what you had written is now classified as garbage, just like the memory on the stack that a local variable has been assigned contains garbage, whatever had just happened to have been written there the last time that location was used, until you initialize it to a known usable value.

    After you have free'd a pointer, it still points to the same location, but that location is no longer available for you to use, so you shouldn't try to use it.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481

    Don't use memory after you free it.


    The size of p is always 8 because 8 bytes is the size of an address on the computer system you used.

    The value of *p remains 55 in your tiny program because the data in memory wasn't overwritten yet.

    On my system the output of program below is
    p, *p: 0x1223010 55
    q, *q: 0x1223010 65453
    p, *p: 0x1223010 65453
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main() {
      int *p,*q;
      p = (int *) malloc(sizeof(int));
      *p = 55;
      printf("p, *p: %p %d\n", p, *p);
      free(p);
      q = (int *)malloc(sizeof(int));
      *q = 65453;
      printf("q, *q: %p %d\n", q, *q);
      printf("p, *p: %p %d\n", p, *p);
      return 0;
    }
    Don't use memory after you free it.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo