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 ()

CodePudding user response:

5250 references 5 floor 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, 33 + 3=36, so the structure's total account for 36 bytes,

Been locked before see, thanks, how much space for the char array c distribution, it is 15 bytes, but 15 bytes nor too convenient access directly, but it is certainly when I visit only 15 bytes, so wondered how 15 bytes CPU to ease of access,

CodePudding user response:

reference 1/f, Old English to catch small's reply:
don't know, I know you last write a semicolon less

Thanks to correct me, neglect,

CodePudding user response:

refer to the second floor truth is right or wrong response:
with the compiler, and project Settings,

Yes, I am not clear what your want to ask is??

CodePudding user response:

refer to 7th floor response:
Quote: refer to 5 floor 5250 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, 33 + 3=36, so the structure's total account for 36 bytes,

Been locked before see, thanks, how much space for the char array c distribution, it is 15 bytes, but 15 bytes nor too convenient access directly, but it is certainly when I visit only 15 bytes, so confused how 15 bytes CPU to ease of access,


Members of the array, each assigned ah char [15] is a continuous distribution of 15 char type of memory,

CodePudding user response:

reference 4 floor qybao response:
example above may not be appropriate, sizeof (char)/2 is 0, but is that the meaning of 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));

Get it!

CodePudding user response:

5250 references to the tenth floor response:
Quote: refer to 7th floor response:

Quote: refer to 5 floor 5250 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, 33 + 3=36, so the structure's total account for 36 bytes,

Been locked before see, thanks, how much space for the char array c distribution, it is 15 bytes, but 15 bytes nor too convenient access directly, but it is certainly when I visit only 15 bytes, so confused how 15 bytes CPU to ease of access,


Members of the array, each assigned ah char [15] is a continuous distribution of 15 char type of memory,

The CPU you want to access data in a c array, read the four bytes, so not is more d read a byte, why not give 16 c distribution, so convenient to read data from c to d

CodePudding user response:

refer to 12th floor response:
Quote: reference to the tenth floor response: 5250
Quote: refer to 7th floor response:

Quote: refer to 5 floor 5250 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, 33 + 3=36, so the structure's total account for 36 bytes,

Been locked before see, thanks, how much space for the char array c distribution, it is 15 bytes, but 15 bytes nor too convenient access directly, but it is certainly when I visit only 15 bytes, so confused how 15 bytes CPU to ease of access,


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related