0
#include<iostream>
using namespace std;
int main()
{
    double a;   
    double b;
    cout<<"Address of a is: "<<&a<<endl;
    cout<<"Address of b is: "<<&b;
}

output is: address of a is: 0x6ffe18 address of b is: 0x6ffe10

Why in the output a variable memory address is greater than b variable memory address even though a is declared first than b.

Should it not be reversed? Im studying c++ so any help is appreciated

  • 2
    Why should it be reversed? Where in your C++ book is written what the address of a variable should be? – wovano Nov 27 '22 at 10:18
  • PS: Run the same code here: https://www.onlinegdb.com/ – wovano Nov 27 '22 at 10:19
  • 2
    The C++ standard makes relationships between addresses only necessary in certain cases. There's no relationship between the memory locations of 2 completely different variables imposed; the compiler is free to choose where to place the variables in memory as it pleases... – fabian Nov 27 '22 at 10:20

0 Answers0