Home > Back-end > C language structure body alignment problem
C language structure body alignment problem
Time:10-05
Struct A { Char a; Int b; Char c [15]; Char d [10]. } A small white consult this memory alignment is what look like?
CodePudding user response:
Don't know, I know you finally write less a semicolon
CodePudding user response:
This follows the compiler, and project Settings,
CodePudding user response:
Memory alignment generally refers to the size in bytes to allocate memory, that is to say, general dissatisfaction with you apply for less than to take up one byte of memory (even with a footprint) For example, Char * c=(char *) malloc (sizeof (char)/2);//wants to apply for at half a byte of memory, but the system automatically according to the actual one byte to allocate memory Perhaps some compilers support memory compression set
CodePudding user response:
The above example may not be appropriate, sizeof (char)/2 is 0, but is that the meaning this Struct A { Int a: 1;//a using only one bit of memory, but the size of a 4 bytes will be allocated memory } A, A. Printf (" % d ", sizeof (a));
CodePudding user response:
Struct A { Char a; Int b; Char c [15]; Char d [10]. }
Char a occupy a byte, allocate memory when the position is 0,1,2,3... Distribution, so the char a position in the 0, Is int, occupy 4 bytes, allocating memory is 0,4,8... Distribution and zero position is occupied now, so the int b at position 4, 4 bytes, 4,5,6,7,1,2,3 is empty, this is one of the byte alignment, C [15] and [10] d similarly occupied [8, 2] and [23], at this time the total structure a total of 33 bytes occupied [0], because of the reason of byte alignment, if the total number of bytes in the structure's biggest byte data type integer times, namely 4 integer times, so 33 + 3=36, this structure's total, a total of 36 bytes,
CodePudding user response:
See your compiler and system, Linux 32-bit system is the default in 4 bytes aligned, for 64 - bit system, it should be 8 byte alignment; Suggest that you write programs in their own environment, assess the sizeof ()