0

I have two AWS lightsail vps boxes, and I am trying the follow simple C program on both vps to test out the address of local variables.

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
        int value = 5;
        char buffer_one[8], buffer_two[8];

        strcpy(buffer_one, "one");
        strcpy(buffer_two, "two");

        printf("buffer_one is at %p\n", buffer_one);
        printf("buffer_two is at %p\n", buffer_two);

        return 0;
}

On box A(running ubuntu 14.04), it produces something like:

buffer_one is at 0x7ffd1fe3d5a0
buffer_two is at 0x7ffd1fe3d5b0

on box B(running Centos 7.5, it's the following:

buffer_one is at 0x7fffa53678a0
buffer_two is at 0x7fffa5367890

I am surprised because both variables are in the main function, which makes them local variables and consequently they should be stored in the "stack" of the program's address space when running. And if so, the first variable(buffer_one) should be in a "higher" address than the second variable(buffer_two). Indeed, it's the case in B, but why A is the other way around...?

xczzhh
  • 658
  • 1
  • 8
  • 22
  • The duplicate asks for different layout for different functions in same program, but the answers are relevant for understanding why the reordering is possible on different or even the same compiler. – Antti Haapala -- Слава Україні Oct 22 '18 at 07:22
  • Good compilers do not slavishly read declarations and assign objects space on the stack in the order they appear in the program. Good compilers read all the source code and seek optimal code and arrangements. That may include grouping objects by size to minimize padding required for alignment, and it may include using the same space for different objects at different times. – Eric Postpischil Oct 22 '18 at 14:21

2 Answers2

1

Besides Elbek's answer, I'd like to add a bit about the reasons.

The compiler only ensures that local variables are on the stack. It's job is just to make sure that the final program executes as what you expect from the source code.

So the actual layout and order is not guaranteed since they don't affect normal program's behaviour. They are up to the implementation of the compiler.

The compiler may be doing some optimizations or may be using different instruction generation techniques. All these might affect the layout (i.e. actual position) of local variables.

And that's why different compilers, even different versions of the same compiler, have different results.

Hoblovski
  • 328
  • 1
  • 9
0

Check your gcc version in both machines. May be because of different versions.

ubuntu 14.04 probably has gcc-4.8 (>= 4.8.2-5~)

centos 7.5 probably has gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-16)

A small difference. But may be this is the reason

Community
  • 1
  • 1
Elbek
  • 616
  • 1
  • 4
  • 15
  • thanks for the answer, in my case, the ubuntu box(A) has 4.8.4, while centos(B) has 4.8.5. It looks like a small version difference to me... – xczzhh Oct 22 '18 at 06:16