Are pointer addresses set every time a program runs in C?

I try to understand pointers, and I stumbled upon this piece of code, and whenever I compile and execute it, the address changes. Is this some kind of unwanted value, or are pointers actually getting allocated memory on the go?

My team:

kaushik@IntelliBox :~/Desktop/Learn_C$ ./Practice nNUmber is equal to : 15 nNumber is equal to : 25 0xbf98fd64 kaushik@IntelliBox :~/Desktop/Learn_C$ make Practice make: 'Practice' is up to date. kaushik@IntelliBox :~/Desktop/Learn_C$ ./Practice nNUmber is equal to : 15 nNumber is equal to : 25 0xbfcce2a4 kaushik@IntelliBox :~/Desktop/Learn_C$ ./Practice nNUmber is equal to : 15 nNumber is equal to : 25 0xbfa25df4 kaushik@IntelliBox :~/Desktop/Learn_C$ ./Practice nNUmber is equal to : 15 nNumber is equal to : 25 0xbfecf104 

My C code:

 #include <stdio.h> int main() { int nNumber; int *pPointer; nNumber = 15; pPointer = &nNumber; printf("nNUmber is equal to : %d\n", nNumber ); *pPointer = 25; printf("nNumber is equal to : %d\n", nNumber ); printf("%p\n", pPointer ); return 0; } 

Thanks in advance.

+5
source share
2 answers

Representation or what exactly constitutes the value of a pointer is an implementation detail. C makes no demands on him. There is no guarantee whether the value will be the same or different each time the code is run.

Only pointer arithmetic between valid pointers (for example, comparing two pointers inside an array object) is defined by the C standard.

By the way, you should point to void* to print with %p , as the C standard requires:

 printf("%p\n", (void*) pPointer ); 

As noted in the comments, some operating systems have randamization of the address space layout . Linux does this by default. For your code, I get the following output with ASLR:

 $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffde18ba7c $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fff981efe0c $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7ffdade6837c $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7ffced208b4c 

If I disable it with:

 echo 0 > /proc/sys/kernel/randomize_va_space 

then it gives the same values:

 $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec $ ./a.out nNUmber is equal to : 15 nNumber is equal to : 25 0x7fffffffeaec 

But as for standard C, there is no guarantee on the values.

+4
source

Is this some kind of unwanted value, or are pointers actually getting allocated memory on the go?

None. The pointer value you specify differs from the fact that the pointed object address ( nNumber ) is different each time the program is launched, or because the pointer representation style used provides different representations for the same address or both. In practice, the former has a much higher probability.

The nNumber address is a function in which a program is loaded into (virtual) memory, and nothing requires coordination from start to start. Indeed, as Jeff Mercado notes in the comments, there is a mechanism called “randomization of the address space layout,” which, when used, intentionally randomizes the download addresses of programs and libraries to increase system security. Its use is a plausible and rather likely explanation for your observation, but it is by no means the only possible one.

+1
source

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


All Articles