简体   繁体   English

在C中的地址零处初始化变量

[英]Initializing variable at address zero in C

This may be a pretty basic question. 这可能是一个非常基本的问题。 I understand that there is a C convention to set the value of null pointers to zero. 我知道有C约定将空指针的值设置为零。 Is it possible that you can ever allocate space for a new variable in Windows, and the address of that allocated space happens to be zero? 您是否有可能在Windows中为新变量分配空间,而该分配空间的地址恰好为零? If not, what usually occupies that address region? 如果不是,通常在那个地址区域中占用什么?

On MS-DOS the null pointer is a fairly valid pointer and due to the OS running in real mode it was actually possible to overwrite the 0x0 address with garbage and corrupt the kernel. 在MS-DOS上,空指针是一个相当有效的指针,并且由于操作系统以实模式运行,因此实际上有可能用垃圾覆盖0x0地址并破坏内核。 You could do something like: 您可以执行以下操作:

int i;
unsigned char* ptr = (unsigned char *)0x0;
for(i = 0; i < 1024; i++)
    ptr[i] = 0x0;

Modern operating systems (eg Linux, Windows) run in protected mode which never gives you direct access to physical memory. 现代操作系统(例如Linux,Windows)在受保护的模式下运行,该模式永远无法直接访问物理内存。

The processor will map the physical addresses to virtual addresses that your program will make use of. 处理器会将物理地址映射到程序将要使用的虚拟地址

It also keeps track of what you access and dare you touch something not belonging to you will you be in trouble (your program will segfault ). 它还会跟踪您访问的内容,并敢于碰触不属于您的东西,否则您会遇到麻烦(您的程序将出现segfault )。 This most definitely includes trying to dereference the 0x0 address. 这绝对包括尝试取消引用0x0地址。

When you "set the value of a pointer to zero" as in 当您“将指针的值设置为零”时,如下所示

int *p = 0;

it will not necessarily end up pointing to physical address zero, as you seem to believe. 您似乎相信,它不一定会最终指向物理地址零。 When a pointer is assigned a constant zero value (or initialized with it), the compiler is required to recognize that situation and treat it in a special way. 当为指针分配一个恒定的零值 (或用其初始化)时,要求编译器识别该情况并以特殊方式对其进行处理。 The compiler is required to replace that zero with implementation-dependent null-pointer value. 要求编译器将零替换为与实现相关的空指针值。 The latter does not necessarily point to zero address. 后者不一定指向零地址。

Null pointer value is supposed to be represented by a physical address that won't be used for any other purpose. 空指针值应该由不会用于任何其他目的的物理地址表示。 If in some implementation physical address zero is a usable address, then such implementation will have to use a different physical address to represent null pointers. 如果在某些实现中物理地址零是可用地址,则这种实现将必须使用其他物理地址来表示空指针。 For example, some implementation might use address 0xFFFFFFFF for that purpose. 例如,某些实现可能为此目的使用地址0xFFFFFFFF In such implementation the initialization 在这种实现中,初始化

int *p = 0;

will actually initialize p with physical 0xFFFFFFFF , not with physical zero. 实际上将使用物理0xFFFFFFFF而不是物理零初始化p

PS You might want to take a look at the FAQ: http://c-faq.com/null/index.html , which is mostly dedicated to exactly that issue. PS:您可能想看看常见问题解答: http : //c-faq.com/null/index.html ,该文章主要致力于解决该问题。

The value 0 has no special meaning. 0没有特殊含义。 It is a convention to set a pointer to 0 and the C compiler has to interpret it accordingly. 按照惯例,将指针设置为0,C编译器必须相应地解释它。 However, there is no connection to the physical address 0 and in fact, that address can be a valid address. 但是,没有连接到物理地址0,实际上,该地址可以是有效地址。 In many systems though the lower adresses are containing hardware related adresses, like interrupt vectors or other. 在许多系统中,尽管较低的地址包含与硬件相关的地址,例如中断向量或其他。 On the Amiga for example, the address 4 was the entry point into the operating system, which is also an arbitrary decision. 例如,在Amiga上,地址4是操作系统的入口点,这也是一个任意决定。

If the address of allocated space is zero, there is insufficient memory available. 如果分配的空间的地址为零,则可用内存不足。 That means your variable could not be allocated. 这意味着您的变量无法分配。

The address at 0x0 is where the CPU starts executing when you power it on. 开机时,CPU从0x0开始执行地址。 Usually at this address there's a jump to the BIOS code and IIRC the first 64K (or more) are reserved for other tasks (determined by the BIOS/UEFI). 通常在此地址上会跳转到BIOS代码,并且IIRC的前64K(或更多)保留用于其他任务(由BIOS / UEFI确定)。 It's an area which is not accessbile by an application. 这是应用程序无法访问的区域。

Given that it should be clear that you cannot have a variable at address 0x0 in Windows. 应当清楚,在Windows中不能在地址0x0处有变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM