Changing the starting address of an array

Pages: 12
It's fun trying to work it out though.

cpp.sh seems to be allocating a memory block that is slightly larger than needed. (Padding for alignment? 16 bytes?)

Results below from whatever compiler is used by cpp.sh (different on my own PC, but still increments *(A-2) every 4 ints):
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
   for ( int n = 1; n <= 40; n++ )
   {
      int *A = new int[n]{};
      std::cout << n << ' ' << *(A-1) << ' ' << *(A-2) << "     Predict (cpp.sh): " << 33 + ( n - 3 ) / 4 * 16 << '\n';
      delete [] A;
   }
}


1 0 33     Predict (cpp.sh): 33
2 0 33     Predict (cpp.sh): 33
3 0 33     Predict (cpp.sh): 33
4 0 33     Predict (cpp.sh): 33
5 0 33     Predict (cpp.sh): 33
6 0 33     Predict (cpp.sh): 33
7 0 49     Predict (cpp.sh): 49
8 0 49     Predict (cpp.sh): 49
9 0 49     Predict (cpp.sh): 49
10 0 49     Predict (cpp.sh): 49
11 0 65     Predict (cpp.sh): 65
12 0 65     Predict (cpp.sh): 65
13 0 65     Predict (cpp.sh): 65
14 0 65     Predict (cpp.sh): 65
15 0 81     Predict (cpp.sh): 81
16 0 81     Predict (cpp.sh): 81
17 0 81     Predict (cpp.sh): 81
18 0 81     Predict (cpp.sh): 81
19 0 97     Predict (cpp.sh): 97
20 0 97     Predict (cpp.sh): 97
21 0 97     Predict (cpp.sh): 97
22 0 97     Predict (cpp.sh): 97
23 0 113     Predict (cpp.sh): 113
24 0 113     Predict (cpp.sh): 113
25 0 113     Predict (cpp.sh): 113
26 0 113     Predict (cpp.sh): 113
27 0 129     Predict (cpp.sh): 129
28 0 129     Predict (cpp.sh): 129
29 0 129     Predict (cpp.sh): 129
30 0 129     Predict (cpp.sh): 129
31 0 145     Predict (cpp.sh): 145
32 0 145     Predict (cpp.sh): 145
33 0 145     Predict (cpp.sh): 145
34 0 145     Predict (cpp.sh): 145
35 0 161     Predict (cpp.sh): 161
36 0 161     Predict (cpp.sh): 161
37 0 161     Predict (cpp.sh): 161
38 0 161     Predict (cpp.sh): 161
39 0 177     Predict (cpp.sh): 177
40 0 177     Predict (cpp.sh): 177
Last edited on
Registered users can post here. Sign in or register to post.
Pages: 12