free()如何知道动态数组中应该释放多少个元素? [英] How do free() know how many elements should be freed in a dynamic array?

查看:89
本文介绍了free()如何知道动态数组中应该释放多少个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *


free()如何知道在动态

数组中应该释放多少元素?


当一个变量空闲时,内存的字节数可以从变量本身的类型中检索出来。


现在,假设那里是一个由多个元素组成的数组需要被释放,因为free()不接受指示大小的参数,怎么能

free()是意识到元素的数量?如第20行所示。


谢谢


lovecreatesbeauty

* /


1 typedef struct

2 {

3 char account_name [200];

4双余额;

5}帐号;

6

7 int main(无效)

8 {

9 int ret = 0;

10 const int ARR_CNT = 200;

11 account * pacc;

12 account * pacc_arr;

13

14 / *分配单个对象* /

15 pacc = malloc(sizeof(* pacc));

16免费(pacc); / *尺寸信息。通过变量类型检索

pacc * /



18 / *分配对象数组* /

19 pacc_arr = malloc(ARR_CNT * sizeof(* pacc));

20 free(pacc_arr); / *怎么可以free()知道'ARR_CNT''* /

21返回ret;

22}

~

~

/*

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" <螺*************** @ gmail.com>在消息中写道

news:11 ********************* @ g47g2000cwa.googlegro ups.com ...
"lovecreatesbeauty" <lo***************@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
/ *

free()如何知道应该在动态数组中释放多少个元素?
/*

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




您忘了查看常见问题解答。
http://c-faq.com/


你的(常见问题)问题的答案是第7节



-Mike



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

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

-Mike


std没有说明malloc等是如何工作的,但这里有一个共同的精确。


它通常是完成的通过malloc(),calloc()等[内部]保存''记录''

分配了什么。


如果你说malloc()大小'x''。 malloc()将分配x个字节,

并记下分配的内存,通过它返回给你的地址

[通常在链表的节点中 - 返回的地址,已分配的大小]。


例如,它可能会从之前分配的更大的

内存中分配x个字节本身,并且将记住'以后有多大x

,也就是说,当你要求时,它预先分配的内存有多大?它。


当你释放内存时,你将相同的地址传递给free()你从malloc()返回
。 free()通常会引用malloc()可以访问的相同的内部
表,然后确定要释放多少字节

up。


" lovecreatesbeauty" <螺*************** @ gmail.com>在消息中写道

news:11 ********************* @ g47g2000cwa.googlegro ups.com ...
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" <lo***************@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
/ *

free()如何知道在动态数组中应该释放多少个元素?

当释放单个变量时,金额可以从变量本身的类型中检索内存字节。

现在,假设有一个多个元素的数组需要被释放,免费( )不接受指示大小的参数,怎样才能知道元素的数量?如第20行所示。

谢谢

lovecreatesbeauty

* /

1 typedef struct
2 {
3 char account_name [200];
4双重余额;
5}帐号;
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 / *分配单个对象* /
15 pacc = malloc(sizeof(* pacc));
16 free(pacc); / *尺寸信息。通过变量类型检索
pacc * /
17
18 / *分配对象数组* /
19 pacc_arr = malloc(ARR_CNT * sizeof(* pacc));
20免费(pacc_arr); / *怎么可以free()知道'ARR_CNT''* /
21返回ret;
22}

/*

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 }
~
~





Mike Wahler写道:

Mike Wahler wrote:
你忘了检查常见问题。
http://c-faq.com/

您(常见问题)问题的答案是<第7节中的

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

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

-Mike




谢谢,我刚刚阅读了以下部分。


7.26免费如何知道要释放多少字节?


抱歉,我删除了原来的帖子并发布了一个更新的错误

在阅读你的回复之前再次版本,抱歉。



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.


这篇关于free()如何知道动态数组中应该释放多少个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆