You're setting a pointer to 0 (NULL) and then adding 1 to it; then you're converting the result to an int and printing the result. The key piece of knowledge you need here is that when you increment (add 1 to) a pointer, you actually add the size of the pointed-to object -- an int pointer is advanced to point to the next int. Since int is (apparently) 4 bytes on your platform, p is incremented to point to an address 4 bytes past where it starts.
c - What is the value of a NULL address location? - Stack Overflow
c - Address of NULL pointer - Stack Overflow
c - Why is NULL not a valid memory address? - Stack Overflow
What is Null?
You're setting a pointer to 0 (NULL) and then adding 1 to it; then you're converting the result to an int and printing the result. The key piece of knowledge you need here is that when you increment (add 1 to) a pointer, you actually add the size of the pointed-to object -- an int pointer is advanced to point to the next int. Since int is (apparently) 4 bytes on your platform, p is incremented to point to an address 4 bytes past where it starts.
Size of the int in C is (typically) 4 bytes. So incrementing a pointer by one unit means incrementing it's value by sizeof(int).
Also, you aren't printing the value of which pointer is directing (as this would certainly crash your program) but the value of the pointer itself. You should definitely take a look at any pointers tutorial in C (or in general).
A pointer variable is an object that can point to another object. Here int *ptr = NULL; declares ptr as a pointer object, that potentially points to an object of int.
The value initially stored into this pointer object is NULL (it is initialized to NULL, so ptr does not point to any object).
Now, ptr too resides in memory. It needs enough bytes to contain the address of the pointed-to object. So it too needs to have an address. Therefore
ptrevaluates to the address of object thatptrpoints to.&ptrevaluates to the location of theptrobject itself in memory*ptrevaluates to the value of the object thatptrpoints to, if it points to an object. If it does not point to an object, then the behaviour is undefined.
Also, %p needs a void * as the corresponding argument, therefore the proper way to print them is
printf("%p\n", (void *)ptr);
printf("%p\n", (void *)&ptr);
ptr holds the value NULL which is what you assigned.
&ptr is the address to the variable ptr which in your case is 0x7fff3415dc40
Dereferencing NULL is undefined behavior. Anything could happen, and most of the time bad things happen. So be scared.
Some old architectures (VAX ...) permitted you to derefence NULL.
The C11 standard specification (read n1570) does not require the NULL pointer to be all zero bits ( see C FAQ Q5.17); it could be something else, but it should be an address which is never valid so is not obtainable by a successful malloc or by the address-of operator (unary &), in the sense of C11. But it is more convenient to have it so, and in practice most (but not all) C implementations do so.
IIRC, on Linux, you might mmap(2) the page containing (void*)0 with MAP_FIXED, but it is not wise to do so (e.g. because a conforming optimizing compiler is allowed to optimize dereference of NULL).
So (void*)0 is not a valid address in practice (on common processors with some MMU and virtual memory running a good enough operating system!), because it is convenient to decide that it is NULL, and it is convenient to be sure that derefencing it gives a segmentation fault. But that is not required by the C standard (and would be false on cheap microcontrollers today).
A C implementation has to provide some way to represent the NULL pointer (and guarantee that it is never the address of some valid location). That might even be done by a convention: e.g. provide a full 232 bytes address space, but promise to never use address 0 (or whatever address you assigned for NULL, perhaps 42!)
When NULL happens to be derefencable, subtile bugs are not caught by a segmentation fault (so C programs are harder to debug).
Couldn't I invent a new architecture where the memory address 0 is accessible to processes?
You could, but you don't want to do that (if you care about providing any standard conforming C implementation). You prefer to make address 0 be the NULL. Doing otherwise make harder to write C compilers (and standard C libraries). And make that address invalid to the point of giving a segmentation fault when derefencing make debugging (and the life of your users coding in C) easier.
If you dream of weird architectures, read about Lisp machines (and Rekursiv, and iapx 432) and see The circuit less traveled talk at FOSDEM2018 by Liam Proven. It really is instructive, and it is a nice talk.
Making address zero unmapped so that a trap occurs if your program tries to access it is a convenience provided by many operating systems. It is not required by the C standard.
According to the C standard:
NULLis not be the address of any object or function. (Specifically, it requires thatNULLcompare unequal to a pointer to of any object or function.)- If you do apply
*toNULL, the resulting behavior is not defined by the standard.
What this means for you is that you can use NULL as an indicator that a pointer is not pointing to any object or function. That is the only purpose the C standard provides for NULL—to use is tests such as if (p != NULL)…. The C standard does not guarantee that if you use *p when p is NULL that a trap will occur.
In other words, the C standard does not require NULL to provide any trapping capability. It is just a value that is different from any actual pointer, provided just so you have one pointer value that means “not pointing to anything.”
General-purpose operating systems typically arrange for the memory at address zero to be unmapped (and their C implementations define NULL to be (void *) 0 or something similar) specifically so that a trap will occur if you dereference a null pointer. When they do this, they are extended the C language beyond what the specification requires. They deliberately exclude address zero from the memory map of your process to make these traps work.
However, the C standard does not require this. A C implementation is free to leave the memory at address zero mapped, and, when you apply * to a null pointer, there might be data there, and your program could read and/or write that data, if the operating system has allowed it. When this is done, it is most often in code intended to run inside the operating system kernel (such as device drivers, kernel extensions, or the kernel itself) or embedded systems or other special-purpose systems with simple operating systems.
So I use C# and I often see many devs use null.
What and which kind of situation do you use this variable?
I am reading c# guide on programming book and I am on Clearing memory now and I haven't encountered null yet. Should I be worried?
In some MCUs, 0 is a valid address where data may be stored. In C, the NULL macro is implementation defined but typically set to ‘0’ or ‘(void*)0’. In these cases, a pointer to a variable at address 0 compares equal to NULL. This makes NULL pointer checks like ‘assert (ptr != NULL)’ badly formed for this data. Additionally, code that accidentally dereferences a null pointer and modifies the value it points to will corrupt data at address 0.
Is the only portable solution to use an MPU to set a region around address 0 to cause a bus fault when read or modified? If so, what’s a good rule of thumb on the amount of bytes for this region to be?
Also, in C++ and C23 does the nullptr keyword prevent this kind of issue? Thanks
Edit: My chip is a Cortex M7 that has up to 512 KB of tightly coupled memory (TCM) for instructions and data at address 0. TCM isn’t able to be moved to any other address. Unlike with other MCUs, address 0 is not flash. This is the NXP MIMXRT1064 for anyone wondering.
Hello all again,
I have another stupid question here lol, so I'm trying to wrap my head around NULL. Im currently under the impression that NULL is a built in constant that has a value of zero, but what does that actually mean? When would it be appropriate to use null? If someone could explain it in layman's terms that would be super helpful!