Initializing variable at address zero in C

▼魔方 西西 提交于 2020-01-15 12:20:08

问题


This may be a pretty basic question. I understand that there is a C convention to set the value of null pointers to zero. 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? If not, what usually occupies that address region?


回答1:


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. 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 (e.g. Linux, Windows) run in protected mode which never gives you direct access to physical memory.

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). This most definitely includes trying to dereference the 0x0 address.




回答2:


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. In such implementation the initialization

int *p = 0;

will actually initialize p with physical 0xFFFFFFFF, not with physical zero.

P.S. 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.




回答3:


The value 0 has no special meaning. It is a convention to set a pointer to 0 and the C compiler has to interpret it accordingly. However, there is no connection to the physical address 0 and in fact, that address can be a valid address. 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.




回答4:


If the address of allocated space is zero, there is insufficient memory available. That means your variable could not be allocated.




回答5:


The address at 0x0 is where the CPU starts executing when you power it on. 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). 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.



来源:https://stackoverflow.com/questions/17462299/initializing-variable-at-address-zero-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!