Null pointer call

Why can't I dereference a null pointer? That is, why can I not read / write memory whose address is 0?

Does the base pointer of my process have a different address? If so, is there a way to get the lower memory adders available for the default heap of my process?

+6
source share
5 answers

A null pointer is not a pointer to "memory [whose] address is just 0". This is just a special pointer that does not indicate anything valid.

The C language says that there are no requirements for the behavior of a program that shares a null pointer.

+9
source

The VM page, which is located at the address (void *)0x0 or NULL , is not displayed by default on any modern OS, so dereferencing the NULL pointer will result in segmentation violation.

NULL pointers are often used as pointers that do not point anywhere.

Yes, you can get the address of your text, stack and heap. For the stack, this is relatively simple, for text and heap you need to consult /proc/self/smaps (if you have procfs ).

+3
source

Why can't I call 00000 000 000 ? I must do it.

+3
source

A null pointer should be considered something that "indicates nothing", instead of pointing to some memory address corresponding to 0.

+1
source

C 2011 online project

6.3.2.3 Pointers
...
3 An integer constant expression with a value of 0 or such an expression cast for type void * is called a null pointer constant. 66) If the constant of the null pointer is converted to a type of pointer, the resulting pointer, called the null pointer, is guaranteed to compare unequal to the pointer to any object or function .

66) The NULL macro NULL defined as <stddef.h> (and other headers) as a null pointer constant; see 7.19.

The emphasis is mine. NULL defined as an invalid pointer value, which is a well-defined "nowhere." You cannot play it because there is nothing to play. Note that although the null pointer constant is always 0-digit, the value of the null pointer is optional; it could be 0x00000000 or 0xDEADBEEF or something completely different; what about the platform.

TL DR, NULL does not represent address 0 ; it represents "no address".

0
source

Source: https://habr.com/ru/post/969648/


All Articles