Re: Re: GCC 4.8.5 No default memory alignment number

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2019-05-11 22:15 +0800, 郭文峰(Gerald Kwok)wrote:

At first let me re-format your mail...  Please do not use HTML.

> sorry.Let me explain what I mean.

> I wanna know what is default structure packing alignment number.<br/>like
> this: (code on gcc 4.8.5)

> struct test
> {
>     char a;
>     double b;
>     char c;
> };
> sizeof(test);

> if the default structure packing alignment number is 4.  sizeof(test) should
> be 16. but In my experiment it is 24.
> and like this

> struct test
> {
>     char a;
>     long double b;
>     char c;
> };
> sizeof(test);

> if the default structure packing alignment number is 8.  sizeof(test) should
> be 32. but In my experiment it is 48.

> So , I just like to know if gcc didn't have default structure packing
> alignment number.
> Or you can see this, on stack over flow someone have the same question with
> me.
> sorry my English is so bad.

As I said, the alignment is defined in the ABI.  On x86_64-linux it is
http://refspecs.linuxfoundation.org/elf/x86_64-abi-0.95.pdf .

A compiler can not define some "default packing alignment number".  It MUST obey
the ABI by default.  If GCC defined `#pragma pack(4)` by default, its output
(object code) would be incompatible with the output from other compilers, for
example Clang or ICC.  On x86_64-linux the ABI doesn't specify a "default
packing" so we can't do any packing by default.

OTOH, on other systems (for example Windows or Mac) the ABI may require
different alignment.  GCC also implemented these different alignment for those
targets (you can test mingw-gcc on Windows).

And, I have to say, many CSDN posters are totally stupid and irresponsible. 
They just tried some code on Windows, found double was aligned to 4, then they
believe they are clever and tell everybody "4 is the default packing, no matter
your platform"!  Just don't listen to them. 
-- 
Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux