Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do pointer addresses change every time the program is executed in C?

Tags:

c

pointers

I am trying to understand pointers and I came across this piece of code and whenever I compile it and execute it, the address changes. Is it some junk value or do pointers actually get memory allotted on the go?

My command prompt:

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 is:

#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;
}

Thank you in advance.

like image 746
kaushik asp Avatar asked Jan 04 '16 18:01

kaushik asp


2 Answers

The representation or what exactly constitutes a pointer value is an implementation detail. C standard doesn't state any requirements on it. There's no guarantee whether the value will be same or different every time you run the code.

Only pointer arithmetic between valid pointers (e.g. comparing two pointers within an array object) is defined by the C standard.

By the way, you should cast the pointer to void* to print with %p as required by the C standard:

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

As noted in the comments, some operating systems do address space layout randamization. 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 outputs 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 far as C standard is concerned, there's absolutely no guarantee on the values.

like image 162
P.P Avatar answered Nov 15 '22 03:11

P.P


Is it some junk value or do pointers actually get memory allotted on the go?

Neither. The pointer value you are printing is different either because the address of the pointed-to object (nNumber) is different on each run of the program, or because the pointer representation style in use affords different representations for the same address, or both. In practice, the former is far more likely.

The address of nNumber is a function of where the program is loaded into (virtual) memory, and nothing requires that to be consistent from run to run. Indeed, as Jeff Mercado observes in comments, there is a mechanism called "Address Space Layout Randomization" that, where employed, intentionally randomizes program and library load addresses in order to improve system security. Its use is a plausible and fairly likely explanation for your observation, but it is by no means the only possible one.

like image 42
John Bollinger Avatar answered Nov 15 '22 02:11

John Bollinger