开发者

Can a valid pointer be allocated at address 0?

On any of the 'major platforms' (which I'm defining as Windows, Mac and Linux for the purpose of this question,) is it conceivable for a validly allocated pointer to be allocated at address 0 in a programs address space (therefore messing up comparisons to NULL?) Does the standard even allow a compiler/platform to have a valid allocati开发者_开发问答on at address 0?


The C++ standard allows it, but such a pointer will not compare equal to literal 0 (the NULL pointer constant).

User-mode applications in the major OSes, however, will never have a valid pointer at 0, or even in the range -65536 to 65535 (to help detect offsets from a NULL pointer).

For the most part, 0 as a usable address only exists in embedded systems, and very rarely in kernels of PC operating systems. But virtual memory systems reserve it, without any exceptions I've ever heard of.


The NULL-pointer (or rather: the 0 token in a context where a pointer is required) does not necessarily mean the address 0, but rather an address that is known to be invalid on this platform. Comparisons to the null pointer are therefore safe, assuming a compliant compiler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜