I see I'm not the only one confused. Why do you want them to be adjacent, why do you care. Thing is, they most likely are adjacent, you just have'nt realised it.
As DaWei said, the compiler will pack your data as it see fit. Lets look at an example ( I compiled this with Dev-C++):
#include <iostream>
using namespace std;
typedef double Scalar;
int main()
{
Scalar x;
Scalar y;
Scalar z;
cout << &x << endl;
cout << &y << endl;
cout << &z << endl;
} output:
0x22ff70
0x22ff68
0x22ff60
Even though x came first in the source code, it is stored last in memory. They are however packed titely. Considering the fact that a double is eight byte, this came to no surprise.
You cant assume that data you define in your source is sequential.
Also your code is wrong to begin with.
WRONG!!!!!
You have to be carefull when doing arithmetic with pointers or references. Lets look at an example:
int *ptr = 0;
cout << ptr << endl;
ptr++
cout << ptr <<endl;
output:
You'd might think p was 1 after increment, but its 4.
Similarly *(p+2) is element at address 8, the same as p[2]. Its the same with references. This is how your code should be:
Since sizeof(Scalar) is 8, &x + 8 is the eight element of type Scalar after &x. The same as saying address of x pluss 8*8 wich is pluss 40.