0
#include <iostream>
struct foo {
  char c[12];
};

int main()
{
  foo fa;
  foo fb;
  std::cout << &fa << "\n";
  std::cout << &fb << "\n";
}

With GCC 6.2, the output: 0x7fff67cd1ff0 0x7fff67cd1fe0 The difference is 16 bytes

With GCC 8.3, the output: 0x7ffc5fe3cf14 0x7ffc5fe3cf08 The difference is 12 bytes

It seems gcc6.2.0 has different default stack alignment with gcc8.3.0. And I also tried the -mpreferred-stack-boundary=4, but it seems not work.

I want to eliminate the difference between gcc6.2.0 and gcc8.3.0, can anyone tell me that if some options can disable that, thanks.

  • 1
    You may be pleased to know that as of GCC6.4 you get the offset of 12. Or maybe not so pleased since you're so close and yet so far. – user4581301 Oct 14 '21 at 01:31
  • 1
    Unrelated: Cheese hack to get more readable output: `std::cout << (char *)&fa - (char *)&fb << "\n";`. Total UB, but fun UB. – user4581301 Oct 14 '21 at 01:32
  • 1
    *I want to eliminate the difference between gcc6.2.0 and gcc8.3.* -- Sounds like an [XY Problem](https://xyproblem.info/). Why the importance in where the compiler places local variables? There is no guarantee what the compiler does as long as the compiler follows the [as-if](https://en.cppreference.com/w/cpp/language/as_if) rule. – PaulMcKenzie Oct 14 '21 at 01:45
  • Could it be the padding of the struct, instead of stack alignement ... what does sizeof(fa) and sizeof(fb) tells you about their size ? You could pass the -Wpadded flag to check if gcc is padding the structs. – PM Laforest Oct 14 '21 at 02:13
  • sizeof(fa) == sizeof(fb) == 12 for both gcc-6.2.0 and gcc-8.3.0. if I add align attribute for both fa & fb, both gcc-6.2.0 and gcc-8.3.0 will give me the exact same result. ``` foo fa __attribute__ ((aligned (16))); foo f6 __attribute__ ((aligned (16))); ``` – user8449964 Oct 14 '21 at 06:08
  • Looks like it was a bug in 6.2: https://stackoverflow.com/questions/59074271/why-are-all-arrays-aligned-to-16-bytes-on-my-implementation – Jeff Garrett Oct 14 '21 at 14:06

0 Answers0