2

I am using a code sample to check the distance between two integers like in the answer of this question.

int i = 0, j = 0;
std::cout << &i - &j;

From my understanding of the memory representation, these memory addresses of these two variables should be next to each other and the difference should be exactly 1.

To my surprise, running this code with MS compiler in VS2017 prints 3 and running the same code with GCC prints 1.

Why this happens, is something wrong with VS?

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • 7
    What you are doing is undefined behavior. You can't subtract pointers unless they are in the same array. – NathanOliver Oct 15 '19 at 18:20
  • 2
    Memory layout is entirely at the discretion of the compiler for local variables. Only class or struct members have any assurances by the standard. – Mark Ransom Oct 15 '19 at 18:21
  • 2
    "_From my understanding of the memory representation, these memory addresses of these two variables should be next to each other and the difference should be exactly 1._" Where did you get such understanding, from? – Algirdas Preidžius Oct 15 '19 at 18:21
  • @AlgirdasPreidžius I was a bit off with this assumption, thinking that the variables will be placed one after another in memory, but you are right, they can be anywhere. Still, I tend to believe that most of the times in these cases, they will be close to each other. – meJustAndrew Oct 15 '19 at 18:25
  • @meJustAndrew This, still, doesn't answer, the question that I asked: where did you get such understanding? "_Still, I tend to believe that most of the times in these cases, they will be close to each other._" What these beliefs are based on? You just got a result, that contradicts those beliefs.. – Algirdas Preidžius Oct 15 '19 at 18:29
  • " I was a bit off with this assumption, thinking that the variables will be placed one after another in memory" your assumtion is very wrong. It is quite possible that a variable does not reside in memory at all. – Slava Oct 15 '19 at 18:35
  • 1
    @AlgirdasPreidžius I can't recall the moment I got this understanding of memory. For me it just makes sense if the compiler tries to fill the memory continuously, and not pick random empty locations. I got one result that contradicts those beliefs (with a distance of two integers) and one that approves it on another hand. This is why I posted the question. – meJustAndrew Oct 15 '19 at 18:40
  • If you care about the stack Lay-out I would use a disassembled on the compiled object. The layout, padding, order and optional register allocation depends on the compiler (version and settings). – eckes Oct 15 '19 at 18:51

2 Answers2

7

C++ standard does not make any requirements for C++ compilers to allocate variables with automatic storage duration in any particular way, including making them contiguous in memory. In fact, compiler may choose to not allocate any memory to a variable, optimizing it out completely.

That is why subtracting pointers makes sense only when they both point to memory inside the same array, or one element past the end of it. In all other situations, including yours, you get undefined behavior.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

The pointer arithmetic you tried has undefined behavior:

If the pointer P points to the ith element of an array, and the pointer Q points at the jth element of the same array, the expression P-Q has the value i-j, if the value fits in std::ptrdiff_t. Both operands must point to the elements of the same array (or one past the end), otherwise the behavior is undefined. If the result does not fit in std::ptrdiff_t, the behavior is undefined.

Oblivion
  • 7,176
  • 2
  • 14
  • 33