Home > Back-end >  C byte alignment problem
C byte alignment problem

Time:09-24

Look at the program and the PIC
Linux 1 g + + 4.8.1 version

# include & lt; Iostream>
2
3 using namespace STD.
4
5 struct A
6 {
Char a;
8 long double b;
9 int c;
10};
11.
12
13 int main ()
14 {
15 cout & lt; 16 cout & lt; 17 cout & lt; 18
19}

The output



Vs2012 code
# include & lt; Iostream>
using namespace std;
Struct A
{
char a;
Long double b;
Int c;
};






Int main ()
{

cout cout int i;
cin> i;
}

Results:

Analysis: use # pragma pack (n) adjust the byte order of no doubt, but the default byte order is a little small doubt
1 the Internet is GCC 4 byte alignment, vc is 8 byte alignment, according to the analysis results of the first output has a problem,
2 if not in accordance with the default byte alignment, the alignment according to structure the maximum length, exactly the both results, this understanding is wrong?

CodePudding user response:



CodePudding user response:

The above is the g + +

V output

CodePudding user response:

Who can help answer, thank you

CodePudding user response:

This uncertainty or with the compiler or the # pragma pack (1, 4 or 8)

CodePudding user response:

# pack is no problem
# but not to bring a pack of GCC is also the default not 8, 48 is or how?

CodePudding user response:

The default # not pack (4)

CodePudding user response:

GCC sizeof (long double) is equal to the 48?
And, in turn, sizeof (long double) equal to 16, sizeof (A) equal to 48?
GCC should be 64?
You do not have a unified code before and after,

CodePudding user response:

Is right, I didn't delete sizeof B A is 48. The long double is 16, according to the online GCC default 4 bytes aligned understand feeling not ah

CodePudding user response:

System is Centos, forget how much is a, should be 6.4 64

CodePudding user response:

Long double types of element's address will be aligned to sizeof (long double) multiples,

If long double is 16, sizeof (A) by default size is 48, otherwise A A [2], the second element of array b members cannot be aligned to 16 times,

CodePudding user response:

refer to the eighth floor qq_22821311 response:
is right, I didn't delete sizeof B A is 48. The long double is 16, according to the online GCC default 4 byte alignment understand feeling not ah

The default 4 byte alignment must be wrong,

CodePudding user response:

Default is commonly 4, see the compiler version, but your long double is 16, so should be 16 byte alignment

CodePudding user response:

GCC 5. Default x 16 byte alignment, 32/64 of a compiler, 4 x untested,


CodePudding user response:

Thank you
  • Related