我可能永远要访问的地址为零? [英] Could I ever want to access the address zero?

查看:133
本文介绍了我可能永远要访问的地址为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常量0用作C和C空指针++。但随着问题指向一个特定的固定地址似乎有一些可能使用分配固定的地址。是否有过任何可以想象的需要,在任何系统中,对于任何低水平的任务,用于访问地址0

The constant 0 is used as the null pointer in C and C++. But as in the question "Pointer to a specific fixed address" there seems to be some possible use of assigning fixed addresses. Is there ever any conceivable need, in any system, for whatever low level task, for accessing the address 0?

如果有,怎么就解决0是空指针和所有?

If there is, how is that solved with 0 being the null pointer and all?

如果没有,是什么使得它肯定没有这样的需要?

If not, what makes it certain that there is not such a need?

推荐答案

无论是在C和C ++中的空指针值以任何方式连接到物理地址 0 。您使用源$ C ​​$ C恒 0 事实上的设置一个指向空指针值无非是一块更的 语法糖。编译器需要把它翻译成用于特定的平台空指针值的实际物理地址。

Neither in C nor in C++ null-pointer value is in any way tied to physical address 0. The fact that you use constant 0 in the source code to set a pointer to null-pointer value is nothing more than just a piece of syntactic sugar. The compiler is required to translate it into the actual physical address used as null-pointer value on the specific platform.

在换句话说, 0 源$ C ​​$ C,没有物理意义任何责任。它可能是 42 13 ,例如。即语言的作者,如果他们很高兴,本来说得那么你必须做 P = 42 ,以便设置指针来空指针值。同样,这并不意味着物理地址 42 将不得不保留空指针。编译器将需要源$ C ​​$ C P = 42 翻译成机器code,将东西的实际物理空指针值( 0×0000 0xBAAD )插入到指针 p 。这正是它的现在与恒 0

In other words, 0 in the source code has no physical importance whatsoever. It could have been 42 or 13, for example. I.e. the language authors, if they so pleased, could have made it so that you'd have to do p = 42 in order to set the pointer p to null-pointer value. Again, this does not mean that the physical address 42 would have to be reserved for null pointers. The compiler would be required to translate source code p = 42 into machine code that would stuff the actual physical null-pointer value (0x0000 or 0xBAAD) into the pointer p. That's exactly how it is now with constant 0.

另外请注意,既不C和C ++提供了一个严格定义的功能,该功能将允许你到一个特定的物理地址分配给一个指针。因此你对问题一会怎么0地址分配给一个指针正式一直无人接听。你根本就具体地址没有分配给C / C ++的指针。然而,在实现所定义的特性的领域,明确整数到指针转换旨在具有这种效果。所以,你会做如下:

Also note, that neither C nor C++ provides a strictly defined feature that would allow you to assign a specific physical address to a pointer. So your question about "how one would assign 0 address to a pointer" formally has no answer. You simply can't assign a specific address to a pointer in C/C++. However, in the realm of implementation-defined features, the explicit integer-to-pointer conversion is intended to have that effect. So, you'd do it as follows

uintptr_t address = 0;
void *p = (void *) address;

请注意,这是不一样的操作的

Note, that this is not the same as doing

void *p = 0;

后者总是会产生空指针值,而在一般情况下,前者没有。前者通常会产生一个指向物理地址 0 ,这可能是也可能不是给定的平台上的空指针值。

The latter always produces the null-pointer value, while the former in general case does not. The former will normally produce a pointer to physical address 0, which might or might not be the null-pointer value on the given platform.

这篇关于我可能永远要访问的地址为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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