Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How do free() know how many elements should be freed in a dynamic array?

Reply
Thread Tools

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

 
 
Jack Klein
Guest
Posts: n/a
 
      01-15-2006
On Sat, 14 Jan 2006 13:18:19 +1300, Ian <(E-Mail Removed)> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-15-2006
On Fri, 13 Jan 2006 23:27:27 GMT, Logan Shaw
<(E-Mail Removed)> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Jordan Abel
Guest
Posts: n/a
 
      01-15-2006
On 2006-01-15, Jack Klein <(E-Mail Removed)> wrote:
> On Fri, 13 Jan 2006 23:27:27 GMT, Logan Shaw
> <(E-Mail Removed)> 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.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      01-15-2006
Jack Klein wrote:
> On Fri, 13 Jan 2006 23:27:27 GMT, Logan Shaw
> <(E-Mail Removed)> 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 ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How does free() know how many elements should be freed in a dynamic array? lovecreatesbeauty C Programming 2 01-13-2006 04:59 PM
How to know the memory pointed by a ptr is freed? ravi C Programming 72 09-14-2004 01:13 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM
How can I know how many elements are there in multi-dimentional array? Abby C Programming 2 09-16-2003 02:51 AM



Advertisments