关于malloc()的问题 [英] Questions about malloc()

查看:43
本文介绍了关于malloc()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候。

我已经读过在
malloc(some_data)上两次调用free(some_ptr)的错误可能导致程序故障。这是为什么?

谢谢。

Joseph Casey。

解决方案



" Joseph Casey" <乔****** @ indigo.ie>在消息中写道

新闻:e3 ******************* @ news.indigo.ie ...

问候。
我已经读过,在malloc(some_data)上调用free(some_ptr)两次的错误会导致程序故障。这是为什么?
谢谢。




因为它会破坏堆。 ''堆''是一系列连贯的

数据结构,用于跟踪可用内存。如果您在

相同的区块中免费拨打两次,内部管理将搞砸。详细信息(

课程)在很大程度上取决于实际的实施情况,因此您需要支付
的libc。


在堆上,请咨询Knuth TAoCP。




Joseph Casey写道:

问候。
我有读到在malloc(some_data)上两次调用free(some_ptr)的错误会导致程序故障。这是为什么?
谢谢。
约瑟夫凯西。




是的,这听起来很有趣......但它实际上并没有按标准定义!


Acc。标准:

描述 - 免费功能


免费功能导致ptr指向的空间被释放,即
deallocated,是,可供进一步分配。如果ptr

是空指针,则不会发生任何操作。否则,如果参数确实

与calloc,malloc或

realloc函数先前返回的指针不匹配,或者如果该空间已通过调用<解除分配br />
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^

免费或realloc,行为未定义。

^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^


- Ravi


Joseph Casey写道:

问候。
我已经读过在malloc(some_data)上调用free(some_ptr)两次的错误会导致程序故障。这是为什么?
谢谢。




想象一下,你和你的房东都健忘了

并且不保持良好的记录。由于您打算在租金到期的那天度假,

,你可以给房东检查一下这个月的租金。 >
你离开。但是它会滑倒你的思绪(你是健忘的,

还记得吗?记得吗?),在你回来的时候你会给他

另一张支票,并为延迟付款道歉。


几天后,您的银行发给您透支

通知,并向您收取费用。你的房东对弹跳支票抱怨

,给你带来不好的支票费用,

并威胁驱逐,除非你能拿出

现在租。他不会接受你的支票(当然),

但幸运的是你在eBay上卖了一个
过时的奶酪三明治并且你发生了很大的利润你的口袋里有一大笔现金。


你现在已经支付了三次租金,两次罚款

费用,并且与您的房东保持良好的关系

和您的银行。现在,如果房东保持更好的记录

这不会发生 - 但是(从

类比转回标准C)记忆房东。不小心需要
。事实上,许多房东只需要保留最低数量的记录,以追求更高效率的b $ b。他们指望你记住

无论你有没有支付租金,无论你有没有b $ b或没有免费()d记忆。提高你的记忆力或

承受后果。


-
Er ********* @ sun.com


Greetings.
I have read that the mistake of calling free(some_ptr) twice on
malloc(some_data) can cause program malfunction. Why is this?
With thanks.
Joseph Casey.

解决方案


"Joseph Casey" <jo******@indigo.ie> wrote in message
news:e3*******************@news.indigo.ie...

Greetings.
I have read that the mistake of calling free(some_ptr) twice on
malloc(some_data) can cause program malfunction. Why is this?
With thanks.



Coz it screws the heap. The ''heap'' is a collection of coherent
datastructures keeping track of free memory. If you call free twice on the
same block, the internal administration will messed up. The details (of
course) depend heavily on the actual implementation and hence the libc you
have.

For an insight on heaps, consult Knuth TAoCP.



Joseph Casey wrote:

Greetings.
I have read that the mistake of calling free(some_ptr) twice on
malloc(some_data) can cause program malfunction. Why is this?
With thanks.
Joseph Casey.



Yes its sounds funny ...but its actually un-defined by standard !

Acc. to standard:
Description - Free function

The free function causes the space pointed to by ptr to be
deallocated, that is, made available for further allocation. If ptr
is a null pointer, no action occurs. Otherwise, if the argument does
not match a pointer earlier returned by the calloc , malloc , or
realloc function, or if the space has been deallocated by a call to
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
free or realloc , the behavior is undefined.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- Ravi


Joseph Casey wrote:

Greetings.
I have read that the mistake of calling free(some_ptr) twice on
malloc(some_data) can cause program malfunction. Why is this?
With thanks.



Imagine that both you and your landlord are forgetful
and unskilled at keeping good records. Since you intend
to be away on vacation on the day when your rent is due,
you give your landlord a check for the month''s rent before
you leave. But it slips your mind (you''re forgetful,
remember? REMEMBER?), and upon your return you give him
another check and an apology for the late payment.

A few days later, your bank sends you an overdraft
notice and socks you with a fee. Your landlord complains
about the bounced check, socks you with a bad-check fee,
and threatens eviction unless you can come up with the
rent Right Now. He won''t accept your check (of course),
but fortunately you''ve made a big profit by selling a
stale cheese sandwich on eBay and you happen to have a
wad of cash in your pocket.

You''ve now paid the rent three times, paid two penalty
fees, and loused up your relationships with your landlord
and your bank. Now, if the landlord kept better records
this wouldn''t have happened -- but (switching from the
analogy back to Standard C) the memory "landlord" isn''t
required to be that careful. In fact, many "landlords"
do only the minimum amount of record-keeping, in pursuit
of greater efficiency. They''re counting on you to remember
whether you have or haven''t paid the rent, whether you have
or haven''t free()d the memory. Sharpen your memory or
suffer the consequences.

--
Er*********@sun.com


这篇关于关于malloc()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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