3
#include <stdio.h>

main()
{
    int i = 5;
    printf("%d \n" , &i);
}

Does the repeated execution of the above program result in different addresses for the variable i?

Dale Hagglund
  • 16,074
  • 4
  • 30
  • 37
shiva
  • 47
  • 2
  • 5
    The address-of operator give you a *pointer*, and the [`printf`](http://en.cppreference.com/w/c/io/fprintf) format code `"%d"` is for *integers*. If you want to print the value of a pointer (i.e. the address contained in a pointer) you need to first cast the pointer to `void *` then use the format `"%p"`. – Some programmer dude Aug 01 '14 at 02:33
  • 3
    As for your question, the answer is *maybe*. It depends on the compiler, compiler version, operating system, operating system version, hardware, and many other factors. There is no definitive "yes" or "no" answer to your question. – Some programmer dude Aug 01 '14 at 02:34
  • I would go further and say that will probably result in different addresses each time. It is unlikely that the program will be loaded in the same memory location each time, but then again no one can say it won't. – Andrew Johnson Aug 01 '14 at 02:38

3 Answers3

9

Yes it can. Here's an explanation from a similar question:

It signifies that your program is being loaded a different (virtual) address each time you run it. This is a feature called Address Space Layout Randomization (ASLR) and is a feature of most modern operating systems.

from here: Why address of a variable change after each execution in C?

Community
  • 1
  • 1
tpdietz
  • 1,358
  • 9
  • 17
1

Yes it will change!

Pointers are used to store the address of a variable and address of a variable may change every time you execute the program.

user3898160
  • 539
  • 4
  • 14
0

Yes for the most every time a program gets loaded into Virtual Memory by your operating system the chances are the memory address of the program will get relocated every single time. So hence every time you print address of the variable it will be different every time it runs.