How do free() know how many elements should be freed in a dynamic array?

Discussion in 'C Programming' started by lovecreatesbeauty, Jan 13, 2006.

  1. /*

    How do free() know how many elements should be freed in a dynamic
    array?

    When free a single variable, the amount of byte of memory can be
    retrieved from the type of variable itself.

    Now, suppose there is an array of more than one element need to be
    freed, as free() doesn't accept a argument indicating the size, how can
    free() be aware of the count of elements? As shown in line #20.

    Thank you

    lovecreatesbeauty

    */

    1 typedef struct
    2 {
    3 char account_name[200];
    4 double balance;
    5 } account;
    6
    7 int main(void)
    8 {
    9 int ret = 0;
    10 const int ARR_CNT = 200;
    11 account *pacc;
    12 account *pacc_arr;
    13
    14 /* to allocate single object */
    15 pacc = malloc(sizeof(*pacc));
    16 free(pacc); /* size info. retrieved via type of variable
    pacc */
    17
    18 /* to allocate object array */
    19 pacc_arr = malloc(ARR_CNT * sizeof(*pacc));
    20 free(pacc_arr); /* how can free() be aware of `ARR_CNT' */
    21 return ret;
    22 }
    ~
    ~
     
    lovecreatesbeauty, Jan 13, 2006
    #1
    1. Advertising

  2. lovecreatesbeauty

    Mike Wahler Guest

    Re: [FAQ] How do free() know how many elements should be freed in a dynamic array?

    "lovecreatesbeauty" <> wrote in message
    news:...
    > /*
    >
    > How do free() know how many elements should be freed in a dynamic
    > array?


    You forgot to check the FAQ.
    http://c-faq.com/

    The answer to your (frequently asked) question is
    in section 7

    -Mike
     
    Mike Wahler, Jan 13, 2006
    #2
    1. Advertising

  3. lovecreatesbeauty

    pemo Guest

    The std says nothing about how malloc etc work, but here's a common precise.

    It's usually done by malloc(), calloc() et al [internally] keeping 'records'
    of what's been allocated.

    Say if you malloc() something of size 'x'. malloc() will allocate x bytes,
    and keep a note of the memory allocated, via the address it returned to you
    [usually in a node of a linked-list - address returned, size allocated].

    For example, perhaps it will allocate x bytes out of a larger chunk of
    memory it's previously allocated itself, and will 'remember' how big x is
    for later, i.e., how big a part of the previoiusly allocated memory it
    previouisly returned when you asked for it.

    When you release memory, you pass the same address to free() that you got
    back from malloc(). free() will typically reference the same internal
    tables that malloc() has access to, to then determine how many bytes to free
    up.



    "lovecreatesbeauty" <> wrote in message
    news:...
    > /*
    >
    > How do free() know how many elements should be freed in a dynamic
    > array?
    >
    > When free a single variable, the amount of byte of memory can be
    > retrieved from the type of variable itself.
    >
    > Now, suppose there is an array of more than one element need to be
    > freed, as free() doesn't accept a argument indicating the size, how can
    > free() be aware of the count of elements? As shown in line #20.
    >
    > Thank you
    >
    > lovecreatesbeauty
    >
    > */
    >
    > 1 typedef struct
    > 2 {
    > 3 char account_name[200];
    > 4 double balance;
    > 5 } account;
    > 6
    > 7 int main(void)
    > 8 {
    > 9 int ret = 0;
    > 10 const int ARR_CNT = 200;
    > 11 account *pacc;
    > 12 account *pacc_arr;
    > 13
    > 14 /* to allocate single object */
    > 15 pacc = malloc(sizeof(*pacc));
    > 16 free(pacc); /* size info. retrieved via type of variable
    > pacc */
    > 17
    > 18 /* to allocate object array */
    > 19 pacc_arr = malloc(ARR_CNT * sizeof(*pacc));
    > 20 free(pacc_arr); /* how can free() be aware of `ARR_CNT' */
    > 21 return ret;
    > 22 }
    > ~
    > ~
    >
     
    pemo, Jan 13, 2006
    #3
  4. Mike Wahler wrote:
    > You forgot to check the FAQ.
    > http://c-faq.com/
    >
    > The answer to your (frequently asked) question is
    > in section 7
    >
    > -Mike


    Thank you, I just read the following section.

    7.26 How does free know how many bytes to free?

    Sorry, I made a mistake to remove my original post and post one updated
    version again before read your reply, sorry.
     
    lovecreatesbeauty, Jan 13, 2006
    #4
  5. >How do free() know how many elements should be freed in a dynamic
    >array?


    It just knows. Perhaps it's in a SQL database in a dungeon in
    Microsoft's corporate headquarters. It's not your problem
    HOW it knows.

    >When free a single variable, the amount of byte of memory can be
    >retrieved from the type of variable itself.


    No, free() is not passed the type of the variable (except perhaps
    by compiler magic).

    >Now, suppose there is an array of more than one element need to be
    >freed, as free() doesn't accept a argument indicating the size, how can
    >free() be aware of the count of elements? As shown in line #20.


    It's up to it to know. It's not obligated to tell you.
    Sometimes, the length or a pointer to the next byte after the
    memory segment you allocated is found immediately *BEFORE* the
    memory pointed at.

    Gordon L. Burditt
     
    Gordon Burditt, Jan 13, 2006
    #5
  6. lovecreatesbeauty

    Logan Shaw Guest

    Re: How do free() know how many elements should be freed in a dynamicarray?

    Gordon Burditt wrote:
    >> When free a single variable, the amount of byte of memory can be
    >> retrieved from the type of variable itself.

    >
    > No, free() is not passed the type of the variable (except perhaps
    > by compiler magic).


    I was thinking about that, but is there any standard that forbids
    a C compiler from passing around dynamic type information with
    pointers? It would certainly be atypical, but it seems like it
    could be done.

    - Logan
     
    Logan Shaw, Jan 13, 2006
    #6
  7. Re: How do free() know how many elements should be freed in adynamic array?

    Logan Shaw <> writes:
    > Gordon Burditt wrote:
    >>> When free a single variable, the amount of byte of memory can be
    >>> retrieved from the type of variable itself.

    >> No, free() is not passed the type of the variable (except perhaps
    >> by compiler magic).

    >
    > I was thinking about that, but is there any standard that forbids
    > a C compiler from passing around dynamic type information with
    > pointers? It would certainly be atypical, but it seems like it
    > could be done.


    Sure, an implementation can decorate a pointer with any additional
    information it likes, as long as it implements the semantics required
    by the language.

    For malloc() and free(), this would be difficult to do, since they
    work with void* pointers. malloc()'s argument specifies only the size
    in bytes, not the type.

    Conceivably an implementation could perform some magic, so that

    foo_t *ptr = malloc(sizeof *ptr);

    would implicitly pass some additional type information into malloc().
    But it would still have to deal with cases where no such information
    is available:

    void *ptr = malloc(42);

    --
    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, Jan 13, 2006
    #7
  8. lovecreatesbeauty

    Ian Guest

    Re: How do free() know how many elements should be freed in a dynamicarray?

    Logan Shaw wrote:
    > Gordon Burditt wrote:
    >
    >>> When free a single variable, the amount of byte of memory can be
    >>> retrieved from the type of variable itself.

    >>
    >>
    >> No, free() is not passed the type of the variable (except perhaps
    >> by compiler magic).

    >
    >
    > I was thinking about that, but is there any standard that forbids
    > a C compiler from passing around dynamic type information with
    > pointers? It would certainly be atypical, but it seems like it
    > could be done.
    >

    That would require the target platform's malloc and free to provide and
    interpret this information, which is beyond the scope of the compiler,
    it's a target implementation detail.

    Ian
     
    Ian, Jan 14, 2006
    #8
  9. Re: How do free() know how many elements should be freed in a dynamicarray?

    >>> When free a single variable, the amount of byte of memory can be
    >>> retrieved from the type of variable itself.

    >>
    >> No, free() is not passed the type of the variable (except perhaps
    >> by compiler magic).

    >
    >I was thinking about that, but is there any standard that forbids
    >a C compiler from passing around dynamic type information with
    >pointers? It would certainly be atypical, but it seems like it
    >could be done.


    No, I don't think extra type information is forbidden, but I don't
    think there is any way to ensure that it will *NEVER* be missing,
    either. Casts to (void *) mustn't remove it, for example. And
    what happens when pointer arithmetic is applied?

    Gordon L. Burditt
     
    Gordon Burditt, Jan 14, 2006
    #9
  10. lovecreatesbeauty

    Ian Guest

    Re: How do free() know how many elements should be freed in a dynamicarray?

    Gordon Burditt wrote:
    >>>>When free a single variable, the amount of byte of memory can be
    >>>>retrieved from the type of variable itself.
    >>>
    >>>No, free() is not passed the type of the variable (except perhaps
    >>>by compiler magic).

    >>
    >>I was thinking about that, but is there any standard that forbids
    >>a C compiler from passing around dynamic type information with
    >>pointers? It would certainly be atypical, but it seems like it
    >>could be done.

    >
    >
    > No, I don't think extra type information is forbidden, but I don't
    > think there is any way to ensure that it will *NEVER* be missing,
    > either. Casts to (void *) mustn't remove it, for example. And
    > what happens when pointer arithmetic is applied?
    >

    The same can be said for any dynamically allocated memory, the only
    legal thing to pass to free is something obtained form malloc.

    Casts don't change anything.

    Ian
     
    Ian, Jan 14, 2006
    #10
  11. lovecreatesbeauty

    Jack Klein Guest

    Re: How do free() know how many elements should be freed in a dynamicarray?

    On Sat, 14 Jan 2006 13:18:19 +1300, Ian <> wrote
    in comp.lang.c:

    > Logan Shaw wrote:
    > > Gordon Burditt wrote:
    > >
    > >>> When free a single variable, the amount of byte of memory can be
    > >>> retrieved from the type of variable itself.
    > >>
    > >>
    > >> No, free() is not passed the type of the variable (except perhaps
    > >> by compiler magic).

    > >
    > >
    > > I was thinking about that, but is there any standard that forbids
    > > a C compiler from passing around dynamic type information with
    > > pointers? It would certainly be atypical, but it seems like it
    > > could be done.
    > >

    > That would require the target platform's malloc and free to provide and
    > interpret this information, which is beyond the scope of the compiler,
    > it's a target implementation detail.


    Well, yes it is, but Logan's question is whether an implementation
    would be allowed to do this, and of course the answer is yes, it
    would.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 15, 2006
    #11
  12. lovecreatesbeauty

    Jack Klein Guest

    On Fri, 13 Jan 2006 23:27:27 GMT, Logan Shaw
    <> wrote in comp.lang.c:

    > Gordon Burditt wrote:
    > >> When free a single variable, the amount of byte of memory can be
    > >> retrieved from the type of variable itself.

    > >
    > > No, free() is not passed the type of the variable (except perhaps
    > > by compiler magic).

    >
    > I was thinking about that, but is there any standard that forbids
    > a C compiler from passing around dynamic type information with
    > pointers? It would certainly be atypical, but it seems like it
    > could be done.
    >
    > - Logan


    There is nothing in the standard that forbids it, and in fact it would
    make for an interesting debug implementation of the memory management
    functions.

    Imagine an implementation that kept a table of the addresses it
    returned from malloc(), calloc(), and realloc(), and erased them when
    they were freed. When a call was made to realloc() or free(), if
    could search the table and if the passed pointer was not found,
    generate an error message and terminate the program.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 15, 2006
    #12
  13. lovecreatesbeauty

    Jordan Abel Guest

    On 2006-01-15, Jack Klein <> wrote:
    > On Fri, 13 Jan 2006 23:27:27 GMT, Logan Shaw
    > <> wrote in comp.lang.c:
    >
    >> Gordon Burditt wrote:
    >> >> When free a single variable, the amount of byte of memory can be
    >> >> retrieved from the type of variable itself.
    >> >
    >> > No, free() is not passed the type of the variable (except perhaps
    >> > by compiler magic).

    >>
    >> I was thinking about that, but is there any standard that forbids
    >> a C compiler from passing around dynamic type information with
    >> pointers? It would certainly be atypical, but it seems like it
    >> could be done.
    >>
    >> - Logan

    >
    > There is nothing in the standard that forbids it, and in fact it would
    > make for an interesting debug implementation of the memory management
    > functions.
    >
    > Imagine an implementation that kept a table of the addresses it
    > returned from malloc(), calloc(), and realloc(), and erased them when
    > they were freed. When a call was made to realloc() or free(), if
    > could search the table and if the passed pointer was not found,
    > generate an error message and terminate the program.


    Or generate a warning message and ignore.
     
    Jordan Abel, Jan 15, 2006
    #13
  14. lovecreatesbeauty

    Joe Wright Guest

    Re: How do free() know how many elements should be freed in a dynamicarray?

    Jack Klein wrote:
    > On Fri, 13 Jan 2006 23:27:27 GMT, Logan Shaw
    > <> wrote in comp.lang.c:
    >
    >
    >>Gordon Burditt wrote:
    >>
    >>>>When free a single variable, the amount of byte of memory can be
    >>>>retrieved from the type of variable itself.
    >>>
    >>>No, free() is not passed the type of the variable (except perhaps
    >>>by compiler magic).

    >>
    >>I was thinking about that, but is there any standard that forbids
    >>a C compiler from passing around dynamic type information with
    >>pointers? It would certainly be atypical, but it seems like it
    >>could be done.
    >>
    >> - Logan

    >
    >
    > There is nothing in the standard that forbids it, and in fact it would
    > make for an interesting debug implementation of the memory management
    > functions.
    >
    > Imagine an implementation that kept a table of the addresses it
    > returned from malloc(), calloc(), and realloc(), and erased them when
    > they were freed. When a call was made to realloc() or free(), if
    > could search the table and if the passed pointer was not found,
    > generate an error message and terminate the program.
    >


    I have done exactly that. I wrote a small 'library' I call Garbage
    Eliminator (ge.c, ge.h) which traps the *alloc() calls and places their
    results in a linked list. free() is also trapped and looks for its
    argument in the list. If found, the list node is removed and the system
    free() is called. If not found, it simply returns without comment.

    I added a 'size_t size(void *p);' which will search my list for p and if
    found return the argument to *alloc().

    I also added 'void freeall(void);' which will free everything in the
    list and then delete the list itself, and report for duty anew.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jan 15, 2006
    #14
    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. Abby
    Replies:
    2
    Views:
    612
  2. ravi
    Replies:
    72
    Views:
    1,487
    RCollins
    Sep 14, 2004
  3. apoorv

    How OS tackles mem. freed by free method

    apoorv, Feb 23, 2005, in forum: C Programming
    Replies:
    3
    Views:
    323
    Richard Bos
    Feb 23, 2005
  4. lovecreatesbeauty
    Replies:
    2
    Views:
    322
    lovecreatesbeauty
    Jan 13, 2006
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    236
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page