Address not consistant

In the following code I have printed the consistent elements from an array and their address.
I want to know why the addresses are not consistent even the elements from a consistent arrangement has been called for?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#define grid 3
using namespace std;

int main ()
{
int j,i;
int	array[grid+1][grid]={{1, 2, 3},{4, 5, 6},{7, 8, 9},{10, 11, 12}};
for (i=0; i<=grid; i++)
 {
  for(j=0; j<grid; j++)
   {
     if (i==1)
       {
 	 cout<<array[i][j]<<endl;
         cout<<&array[i][j]<<endl;
	}
    }
 }
return 0;
}
They look consistent to me. Each int takes up 4 bytes or 8 bytes, the numbers all make sense.

What do you think is inconsistent about them?
the addresses are not consistent
I assume, by consistent you mean consecutive. Running your routine under www.cpp.sh I get (after removing the if (i==1) and a minor merge of the two cout to cout<<array[i][j]<< " - " <<&array[i][j]<<"\n";)
1 - 0x74a78462d8f0
2 - 0x74a78462d8f4
3 - 0x74a78462d8f8
4 - 0x74a78462d8fc
5 - 0x74a78462d900
6 - 0x74a78462d904
7 - 0x74a78462d908
8 - 0x74a78462d90c
9 - 0x74a78462d910
10 - 0x74a78462d914
11 - 0x74a78462d918
12 - 0x74a78462d91c
Thanks this does make sense.
Can you tell me what is mean by the digits and alphabets in the address?
0x74a78462d8f0
It's just a number. It's in hexadecimal.

https://en.wikipedia.org/wiki/Hexadecimal
Thanks @Repeater and @MikeStgt
Will the address be same every time I run the code, or then change from time to time?
It can depend on the C++ implementation and the OS.

On Windows the addresses can vary from run to run. The starting address may vary, but the 4 byte difference between consecutive address will remain the same.
Last edited on
Topic archived. No new replies allowed.