Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
 
lovecreatesbeauty
Guest
Posts: n/a
 
      01-13-2006
/*

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

When it frees a single variable, the size information about 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 }
~
~

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-13-2006

"lovecreatesbeauty" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> /*
>
> How does free() know how many elements should be freed in a dynamic
> array?


Same answer as the last time you asked: Read The FAQ.

-Mike


 
Reply With Quote
 
 
 
 
lovecreatesbeauty
Guest
Posts: n/a
 
      01-13-2006

Mike Wahler wrote:
> "lovecreatesbeauty" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > /*
> >
> > How does free() know how many elements should be freed in a dynamic
> > array?

>
> Same answer as the last time you asked: Read The FAQ.
>
> -Mike


very sorry, it's my fault.

 
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 do free() know how many elements should be freed in a dynamic array? lovecreatesbeauty C Programming 13 01-15-2006 03:58 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