Home > Software engineering >  C/C byte alignment problem
C/C byte alignment problem

Time:10-09

Bug performance: in the program have a c + + class, its an int type variable expression very eerie, clearly assign a value to it, but read data and assign its value, through debugging, found it in the error while reading memory, offset the three bytes, in front of the type int variable declared a bool variable, debugging found if the bool variable on an int type variable, performance is normal, it turned out that in other places in the program (the program is not I write), defines some 1 byte alignment, structure, and then the basic confirmation is byte alignment problem, indeed as expected in front of the c + + class definition with # pragma pack () lines of code, this is normal, however, is confusing, I looked at all the code file, all the # pragma pack (1) eventually connect a line # pragma pack (), the byte alignment set as default in vs byte alignment 8. Here I have a question: since set the byte alignment, the program will be carried out in accordance with the my Settings should be aligned to visit, should not appear memory access errors, why there will be the bug, and the structure of the before weight # pragma pack (1) and # pragma pack () are paired, it may not affect the c + + class, (note: the program compiled using vs editor)
On the code, actually very simple:
 
//c + + class
The class Myclass {
Public:
Mystruct st.
Bool is_connect;
Int id;
}

//other structure definition file:
The # pragma pack (1)
Struct mystruct {
char a;
Short int b;
}
# pragam pack ()

CodePudding user response:

Fyi:
 # include & lt; Stdio. H> 
# define field_offset (s, f) (int) (& amp; (((struct s *) (0)) - & gt; F))
Struct AD {int a; Char b [13]. Double c; };
# pragma pack (push)
The # pragma pack (1)
Struct A1 {int a; Char b [13]. Double c; };
# pragma pack (2)
Struct A2 {int a; Char b [13]. Double c; };
# pragma pack (4)
Struct A4 {int a; Char b [13]. Double c; };
# pragma pack (8)
Struct A8 {int a; Char b [13]. Double c; };
# pragma pack (16)
Struct A16 {int a; Char b [13]. Double c; };
# pragma pack (pop)
Int main () {
Printf (" AD. A % d \ n ", field_offset (AD, a));
Printf (" AD. B % d \ n ", field_offset (AD, b));
Printf (" AD. C % d \ n ", field_offset (AD, c));
Printf (" AD sizeof % d \ n ", sizeof (AD));
printf("\n");
Printf (" A1. A % d \ n ", field_offset (A1, a));
Printf (" A1. B % d \ n ", field_offset (A1, b));
Printf (" A1. C % d \ n ", field_offset (A1, c));
Printf (" A1 sizeof % d \ n ", sizeof (A1));
printf("\n");
Printf (" A2. A % d \ n ", field_offset (A2, a));
Printf (" A2. B % d \ n ", field_offset (A2, b));
Printf (" A2. C % d \ n ", field_offset (A2, c));
Printf (" A2 sizeof % d \ n ", sizeof (A2));
printf("\n");
Printf (" A4. A % d \ n ", field_offset (A4, a));
Printf (" A4. B % d \ n ", field_offset (A4, b));
Printf (" A4. C % d \ n ", field_offset (A4, c));
Printf (" A4 sizeof % d \ n ", sizeof (A4));
printf("\n");
Printf (" A8. A % d \ n ", field_offset (A8, a));
Printf (" A8. B % d \ n ", field_offset (A8, b));
Printf (" A8. C % d \ n ", field_offset (A8, c));
Printf (" A8 sizeof % d \ n ", sizeof (A8));
printf("\n");
Printf (" A16. A % d \ n ", field_offset A16, (a));
Printf (" A16. B % d \ n ", field_offset (A16, b));
Printf (" A16. C % d \ n ", field_offset (A16, c));
A16 sizeof printf (" % d \ n ", sizeof (A16));
printf("\n");
return 0;
}
//AD. A 0
//AD. B 4
//AD. 24 c
//AD sizeof 32
//
//A1. A 0
//A1. B 4
//A1. 17 c
//A1 sizeof 25
//
//A2. A 0
//A2. B 4
//A2. 18 c
//A2 sizeof 26
//
//A4. A 0
//A4. B 4
//A4. 20 c
//A4 sizeof 28
//
//A8. A 0
//A8. B 4
//A8. 24 c
//A8 sizeof 32
//
//A16. A 0
//A16. B 4
//A16. 24 c
//A16 sizeof 32
//
//

CodePudding user response:

Alignment problem must be taken into account in design of memory structure, (4 bytes) if the definition of char and bool % 4 to 0, not total will need to put a placeholder byte, or when access to directly use a pointer,

CodePudding user response:

You are supposed to be mystruct this structure aligned due to inconsistent if refer to the same h are the same alignment should be no problem of the estimation is you read is another default mystruct alignment

CodePudding user response:

With a push pop limit scope

# pragma pack (push, 1)
Struct mystruct {
char a;
Short int b;
};
# pragma pack (pop)

CodePudding user response:

Top up, also had a similar problem,

CodePudding user response:

reference yaozhiyong110 reply: 3/f
you should be to inconsistent mystruct this structure aligned if refer to the same h are the same alignment should be no problem of the estimation is you read is another default alignment mystruct


My thoughts with you about, should be the way reading is another alignment, but didn't find out the problem, I now is in the wrong class above plus 1 # pragma pack (), although temporarily solve the problem, but I don't know the specific problems is?

CodePudding user response:

reference 4 floor zgl7903 response:
with push pop limit scope

# pragma pack (push, 1)
Struct mystruct {
char a;
Short int b;
};
# pragma pack (pop)


The methods in the code should be same with me, because the project has a lot of this set, can't change to push and pop this way,

CodePudding user response:

It is better to modify the structure of 4 byte alignment, although some trouble, but it's worth,
Byte alignment, it is a trouble thing, so a start defining the structure or class, you should consider this problem, in order to reduce the late because the byte alignment and the possibility of a problem,
If the agreement is kind thing is beyond the scope of their own control, you can write a class to implement on the interpretation of the actual structure and assembly, so later can become easier,
use

CodePudding user response:

Suggest using header files, not # pragma because different compilers support command is slightly different:
 # include & lt; Pshpack1. H> 

Typedef struct {
char a;
Short int b;
} mystruct;

# include & lt; Poppack. H>
access offset with offsetof macro not to go,
 mystruct ms; 
Char * PC=(char *) ((UINT_PTR) & amp; Ms + offsetof (mystruct, a));
Ps=short int * (short int *) ((UINT_PTR) & amp; Ms + offsetof (mystruct, b));

CodePudding user response:

nullnullnullnullnullnullnullnullnull
  • Related