doubt about freeing memory

Discussion in 'C Programming' started by orium69@gmail.com, Feb 23, 2006.

  1. Guest

    Hi everyone!

    I have a doubt, when I free an alloced memory, how does the compiler
    know how many bytes I have alloced forward the pointer.

    eg:

    int *coiso;

    coiso=(int *)malloc(sizeof(int)*10);

    free(coiso);

    How the compiler know that it has to free from coiso to
    coiso+(sizeof(int)*10)?

    thanks

    sorry for my probably realy bad english...
    , Feb 23, 2006
    #1
    1. Advertising

  2. wrote:
    > eg:
    >
    > int *coiso;
    >
    > coiso=(int *)malloc(sizeof(int)*10);


    Casting the result of 'malloc' is not a good idea. Why don't you just do

    coiso = malloc(sizeof(int)*10);

    An even better idea would be doing

    coiso = malloc(10 * sizeof *coiso);

    > free(coiso);
    >
    > How the compiler know that it has to free from coiso to
    > coiso+(sizeof(int)*10)?


    Short answer: somehow.

    This is an implementation detail, which you are not supposed to worry about. For
    example, the run-time library might store the size originally passed to 'malloc'
    somewhere in a secret place. 'free' might know how to retrieve that size when
    necessary.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Feb 23, 2006
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > Hi everyone!
    >
    > I have a doubt, when I free an alloced memory, how does the compiler
    > know how many bytes I have alloced forward the pointer.
    >
    > eg:
    >
    > int *coiso;
    >
    > coiso=(int *)malloc(sizeof(int)*10);
    >
    > free(coiso);
    >
    > How the compiler know that it has to free from coiso to
    > coiso+(sizeof(int)*10)?
    >

    It isn't the compilers problem, it is the underlying implementation of
    the memory allocator that keeps track of allocated block sizes.

    --
    Ian Collins.
    Ian Collins, Feb 23, 2006
    #3
  4. writes:
    > I have a doubt, when I free an alloced memory, how does the compiler
    > know how many bytes I have alloced forward the pointer.
    >
    > eg:
    >
    > int *coiso;
    >
    > coiso=(int *)malloc(sizeof(int)*10);
    >
    > free(coiso);
    >
    > How the compiler know that it has to free from coiso to
    > coiso+(sizeof(int)*10)?


    This is question 7.26 in the comp.lang.c FAQ,
    <http://www.c-faq.com/malloc/freesize.html>.

    See also questions 7.7 and 7.7b. While you're at it, you might read
    all of section 7, and the rest of the FAQ as well. It's a very good
    resource.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 23, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajesh.Rapaka

    freeing memory

    Rajesh.Rapaka, Apr 20, 2005, in forum: Java
    Replies:
    17
    Views:
    2,822
    Eric Sosman
    Apr 21, 2005
  2. Harald Kirsch

    freeing memory

    Harald Kirsch, Apr 22, 2005, in forum: Java
    Replies:
    0
    Views:
    417
    Harald Kirsch
    Apr 22, 2005
  3. Hassan Iqbal
    Replies:
    3
    Views:
    478
    Al Bowers
    Sep 25, 2003
  4. freeing memory....partially

    , Oct 4, 2003, in forum: C Programming
    Replies:
    12
    Views:
    552
  5. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    578
    Jean-Claude Arbaut
    Jun 15, 2005
Loading...

Share This Page