How to Represent Memory Address 0 in C/C++

Click the blue “Most Programmer” to follow me!

Add a “star“, every day at 18:03 to learn technology together!

This is a typical case of “knowing the phenomenon but not the reason behind it”, a common issue in our learning process.When teachers or textbooks mention that “accessing address 0 is an illegal access”, how many people ask “Why” and delve deeper into it?Most people start learning C language on a general-purpose operating system, mostly on Windows, with a few on Linux, BSD, or other Unix-like systems. Most of them are on common architectures like x86 or x86_64. Thus, the statement “address 0 is an illegal address” seems to have become a universally accepted fact, akin to the sun being in the south at noon, something that has been passed down through generations. Just as many people are unaware of the sun’s position relative to the equator and the tropics, many computer students fundamentally misunderstand the inaccessibility of address 0.Memory addresses, especially logical addresses, naturally start from address 0 and extend to some positive integer range. Why is there a certain address range that exists but is not allowed for use? Doesn’t it resemble real-life situations where there are places right in front of you that you are not allowed to enter? Such places are likely occupied or reserved, with a high level of “confidentiality”, not allowing you to see or even know about them. The memory address 0 is generally occupied by the boot code or the operating system, which has a much higher privilege level than user processes, thus preventing user programs from accessing it.However, if you come to a piece of unclaimed land, that’s a different story. If there are no existing logical rules, you can use it however you like. You can even treat a block of memory as a simple data storage device, starting to record data from address 0. In the past, we often treated ROM chips as additional data storage devices. For bare-metal development, how to use the memory address 0 depends on the rules of the controller you are using. For example, if the controller starts executing from address 0 by default when powered on, then address 0 must contain the first instruction.For instance, placing a jump instruction at address 0:ORG 0000HLJMP STARTORG 000BHSTART:Or initializing some registers at power-up:ORG 0000HMOV R0, #33HMOV A, R0Or the controller does not start executing from address 0 but requires some data, such as a vector table, to be placed at address 0:ORG 0000HDB 55DB 60HAll of these are set according to the requirements of the control board or controller being used.As for how C/C++ represents memory at address 0, it is naturally represented by the number 0, or in hexadecimal as 0x00 or 0x00000000. Otherwise, how else would you represent address 0? Address 0 is just one of many addresses, just like writing Arabic numerals, where 0 is 0, just as 100 is 100; there is no need to design any special settings for 0. (As for NULL, it does not necessarily equal 0 at all times.)Some may argue that the examples I used are all assembly language; does C/C++ itself not support address 0? Of course not. You need to distinguish between what the language syntax supports and what the compiler that compiles that language supports; these are two different things. As long as the C/C++ compiler you are using supports operations similar to the above, you can certainly use C/C++. The key is that you need to achieve the binary logic you want. If certain operations are not supported even by the assembler, you may even have to manually edit the machine code, which was not impossible in the past.With the development of computer software, integrated development environments have become increasingly powerful, which may lead some students to be unaware of what compilers and integrated tools do for them from start to finish. Address 0 is not some “mysterious” address; as long as it is not occupied by hardware or software, it is just one of many ordinary addresses, and how you program it is just like how you program any other address.How to Represent Memory Address 0 in C/C++

Leave a Comment