Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NULL/0 an illegal memory location for an object?

I understand the purpose of the NULL constant in C/C++, and I understand that it needs to be represented some way internally.

My question is: Is there some fundamental reason why the 0-address would be an invalid memory-location for an object in C/C++? Or are we in theory "wasting" one byte of memory due to this reservation?

like image 617
aioobe Avatar asked Jun 02 '10 18:06

aioobe


People also ask

Is 0 a valid memory address?

Memory address 0 is called the null pointer. Your program is never allowed to look at or store anything into memory address 0, so the null pointer is a way of saying "a pointer to nothing".

Why should you never access the memory location pointed to by a null pointer?

Because a null pointer does not point to a meaningful object, an attempt to dereference (i.e., access the data stored at that memory location) a null pointer usually (but not always) causes a run-time error or immediate program crash.

Why do memory addresses start with 0?

The 0x is just a notation to let you know the number is in hexadecimal form. Same as you'd write 042 for an octal number, or 42 for a decimal one. So - 42 == 052 == 0x2A . The "0" helps the parser know that its dealing with a (constant) number and the "x" stands for hex.

Does the null pointer point to a value of 0?

Why is address zero used for the null pointer in C/C++? Null pointer is a pointer which points nothing. Some uses of null pointer are: b) To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet.


2 Answers

The null pointer does not actually have to be 0. It's guaranteed in the C spec that when a constant 0 value is given in the context of a pointer it is treated as null by the compiler, however if you do

char *foo = (void *)1;
--foo;
// do something with foo

You will access the 0-address, not necessarily the null pointer. In most cases this happens to actually be the case, but it's not necessary, so we don't really have to waste that byte. Although, in the larger picture, if it isn't 0, it has to be something, so a byte is being wasted somewhere

Edit: Edited out the use of NULL due to the confusion in the comments. Also, the main message here is "null pointer != 0, and here's some C/pseudo code that shows the point I'm trying to make." Please don't actually try to compile this or worry about whether the types are proper; the meaning is clear.

like image 58
bobDevil Avatar answered Oct 13 '22 21:10

bobDevil


This has nothing to do with wasting memory and more with memory organization.

When you work with the memory space, you have to assume that anything not directly "Belonging to you" is shared by the entire system or illegal for you to access. An address "belongs to you" if you have taken the address of something on the stack that is still on the stack, or if you have received it from a dynamic memory allocator and have not yet recycled it. Some OS calls will also provide you with legal areas.

In the good old days of real mode (e.g., DOS), all the beginning of the machine's address space was not meant to be written by user programs at all. Some of it even mapped to things like I/O. For instance, writing to the address space at 0xB800 (fairly low) would actually let you capture the screen! Nothing was ever placed at address 0, and many memory controller would not let you access it, so it was a great choice for NULL. In fact, the memory controller on some PCs would have gone bonkers if you tried writing there.

Today the operating system protects you with a virtual address space. Nevertheless, no process is allowed to access addresses not allocated to it. Most of the addresses are not even mapped to an actual memory page, so accessing them will trigger a general protection fault or the equivalent in your operating system. This is why 0 is not wasted - even though all the processes on your machine "have an address 0", if they try to access it, it is not mapped anywhere.

like image 43
Uri Avatar answered Oct 13 '22 22:10

Uri