实现Malloc() [英] Implementing Malloc()

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

问题描述

我们正在讨论实现malloc(),特别是下面的

情况。


假设用户请求1Mb的内存。不幸的是,我们只有
512Kb可用。在这种情况下,大多数mallocs()都会返回null。

绝大多数程序员都不愿意检查malloc()失败

这么小的分配,因此,当NULL指针被取消引用时,程序将以SIGSEGV崩溃为




那么为什么不返回指向512Kb的指针''有空吗?很可能用户永远不会真正写入他分配的内存的一半上限,在这种情况下,程序会有

在崩溃之前继续成功。


最糟糕的事情是程序员_does_写到

mallocated块的结尾。在这种情况下,或者有一个SIGSEGV

再次(没有比以前更差),或者如果512Kb在

的中间,那么堆malloc()是从那时起,写入可能会成功,

并且程序可以继续,尽管有一些可能的小数据

损坏。


malloc()的任何实现都使用这样的策略吗?

============================= ========

McCoy'的诱惑者嘉豪,

和处女他有相当的分数。

他告诉他们,亲爱的,

你是最后的边疆,

男人从未去过的地方。

We were discussing implementing malloc(), in particular the following
situation.

Suppose the user requests 1Mb of memory. Unfortunately, we only have
512Kb available. In this situation, most mallocs() would return null.
The huge majority of programmers won''t bother to check malloc() failure
for such a small allocation, so the program will crash with a SIGSEGV as
soon as the NULL pointer is dereferenced.

So why not just return a pointer to the 512Kb that''s available? It''s
quite possible that the user will never actually write into the upper
half of the memory he''s allocated, in which case the program will have
continued successfully where before it would have crashed.

The worst thing that can happen is that the programmer _does_ write to
the end of the mallocated block. In this case, either there''s a SIGSEGV
again (no worse off than before), or if the 512Kb is in the middle of
the heap malloc() is drawing from then the writes might well succeed,
and the program can continue albeit with some possible minor data
corruption.

Do any implementations of malloc() use a strategy like this?
=====================================
McCoy''s a seducer galore,
And of virgins he has quite a score.
He tells them, "My dear,
You''re the Final Frontier,
Where man never has gone before."

推荐答案

11月26日晚上9:40,CJ< nos ... @nospam.invalidwrote:
On Nov 26, 9:40 pm, CJ <nos...@nospam.invalidwrote:

我们当时讨论实现malloc(),特别是以下

情况。


假设用户请求1Mb的内存。不幸的是,我们只有
512Kb可用。在这种情况下,大多数mallocs()都会返回null。

绝大多数程序员都不愿意检查malloc()失败

这么小的分配,因此,当NULL指针被取消引用时,程序将以SIGSEGV崩溃为




那么为什么不返回指向512Kb的指针''有空吗?很可能用户永远不会真正写入他分配的内存的一半上限,在这种情况下,程序会有

在崩溃之前继续成功。


最糟糕的事情是程序员_does_写到

mallocated块的结尾。在这种情况下,或者有一个SIGSEGV

再次(没有比以前更差),或者如果512Kb在

的中间,那么堆malloc()是从那时起,写入可能会成功,

并且程序可以继续,尽管有一些可能的小数据

损坏。


malloc()的任何实现都使用这样的策略吗?
We were discussing implementing malloc(), in particular the following
situation.

Suppose the user requests 1Mb of memory. Unfortunately, we only have
512Kb available. In this situation, most mallocs() would return null.
The huge majority of programmers won''t bother to check malloc() failure
for such a small allocation, so the program will crash with a SIGSEGV as
soon as the NULL pointer is dereferenced.

So why not just return a pointer to the 512Kb that''s available? It''s
quite possible that the user will never actually write into the upper
half of the memory he''s allocated, in which case the program will have
continued successfully where before it would have crashed.

The worst thing that can happen is that the programmer _does_ write to
the end of the mallocated block. In this case, either there''s a SIGSEGV
again (no worse off than before), or if the 512Kb is in the middle of
the heap malloc() is drawing from then the writes might well succeed,
and the program can continue albeit with some possible minor data
corruption.

Do any implementations of malloc() use a strategy like this?



我希望不是。

I hope not.


CJ写道:
CJ wrote:

我们正在讨论实现malloc(),特别是以下

情况。


假设用户请求1Mb的内存。不幸的是,我们只有
512Kb可用。在这种情况下,大多数mallocs()都会返回null。

绝大多数程序员都懒得查看malloc()

这么小的分配失败,因此,只要取消引用NULL指针,程序就会以

SIGSEGV崩溃。
We were discussing implementing malloc(), in particular the following
situation.

Suppose the user requests 1Mb of memory. Unfortunately, we only have
512Kb available. In this situation, most mallocs() would return null.
The huge majority of programmers won''t bother to check malloc()
failure for such a small allocation, so the program will crash with a
SIGSEGV as soon as the NULL pointer is dereferenced.



这可以由智能程序员安全使用。

This can be used safely by an intelligent programmer.


那么为什么不直接返回指针那可用的512Kb?很可能用户永远不会真正写入他分配的内存的一半上限,在这种情况下,程序会有

在崩溃之前成功继续。


最糟糕的事情是程序员确实写了

mallocated块的结尾。在这种情况下,要么再次出现
SIGSEGV(没有比之前更差),或者如果512Kb位于堆中的
中间,则malloc()是从那时起,写入可能会很好地成功,并且该程序可以继续,尽管有一些可能的b / b $ b $次要数据损坏。
So why not just return a pointer to the 512Kb that''s available? It''s
quite possible that the user will never actually write into the upper
half of the memory he''s allocated, in which case the program will have
continued successfully where before it would have crashed.

The worst thing that can happen is that the programmer does write to
the end of the mallocated block. In this case, either there''s a
SIGSEGV again (no worse off than before), or if the 512Kb is in the
middle of the heap malloc() is drawing from then the writes might
well succeed, and the program can continue albeit with some possible
minor data corruption.



这不可以。


Brian

This cannot.

Brian


CJ写道:
CJ wrote:

我们正在讨论实施malloc(),特别是下面的

情况。


假设用户请求1Mb内存。不幸的是,我们只有
512Kb可用。在这种情况下,大多数mallocs()都会返回null。

绝大多数程序员都不愿意检查malloc()失败

这么小的分配,因此,当NULL指针被取消引用时,程序将以SIGSEGV崩溃为




那么为什么不返回指向512Kb的指针''有空吗?很可能用户永远不会真正写入他分配的内存的一半上限,在这种情况下,程序会有

在崩溃之前继续成功。


最糟糕的事情是程序员_does_写到

mallocated块的结尾。在这种情况下,或者有一个SIGSEGV

再次(没有比以前更差),或者如果512Kb在

的中间,那么堆malloc()是从那时起,写入可能会成功,

并且程序可以继续,尽管有一些可能的小数据

损坏。
We were discussing implementing malloc(), in particular the following
situation.

Suppose the user requests 1Mb of memory. Unfortunately, we only have
512Kb available. In this situation, most mallocs() would return null.
The huge majority of programmers won''t bother to check malloc() failure
for such a small allocation, so the program will crash with a SIGSEGV as
soon as the NULL pointer is dereferenced.

So why not just return a pointer to the 512Kb that''s available? It''s
quite possible that the user will never actually write into the upper
half of the memory he''s allocated, in which case the program will have
continued successfully where before it would have crashed.

The worst thing that can happen is that the programmer _does_ write to
the end of the mallocated block. In this case, either there''s a SIGSEGV
again (no worse off than before), or if the 512Kb is in the middle of
the heap malloc() is drawing from then the writes might well succeed,
and the program can continue albeit with some possible minor data
corruption.



至少在第一种情况下,程序员*可以*检测对

malloc()的调用是否失败。您的解决方案无法做到这一点。使用

解决方案,每次调用malloc()都会出现SIGSEGV

或数据损坏的可能性而不会发出警告。

SM

rot13电子邮件

At least in the first case the programmer *can* detect if the call to
malloc() failed. This is not possible with your solution. With your
solution, every call to malloc() presents the possibility of a SIGSEGV
or data corruption without warning.
SM
rot13 for email


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

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