Gegeben sei folgendes struct student. Geben sie in der nachfolgenden Deklaration die…
- User
Gegeben sei folgendes struct student. Geben sie in der nachfolgenden Deklaration die Größe der Felder, des Paddings und der Löcher in Byte an. Die Längen der primitiven Datentypen können Sie der folgenden Tabelle entnehmen.
Achten Sie auf self-alignment, struct-alignment und das Padding von structs. Das Beispiel wird auf einem x86_64 Linux kompiliert und ausgeführt.
Typ Länge in Byte char 1 short 2 int 4 pointer 8 long double 16
struct student { char matrikelnummer[9]; char* name; unsigned short bonuspunkte; unsigned int punkte_klausur; char note[4]; long double plagiatswahrscheinlichkeit; unsigned short alter; };
struct student { // 0 Byte Padding char matrikelnummer[9]; // Länge 9 Byte // 7 Byte Padding char* name; // Länge 8 Byte // 0 Byte Padding unsigned short bonuspunkte; // Länge 2 Byte // 2 Byte Padding unsigned int punkte_klausur; // Länge 4 Byte // 0 Byte Padding char note[4]; // Länge 4 Byte // 8 Byte Padding long double plagiatswahrscheinlichkeit; // Länge 16 Byte // 0 Byte Padding unsigned short alter; // Länge 2 Byte // ? Byte Padding };
how many bytes is the final padding
- Assistant
To determine the final padding required at the end of the
struct student, we need to ensure that the total size of the struct is a multiple of its alignment requirement. The alignment of the struct is typically the largest alignment of any of its members, which in this case is 8 bytes (assuming thelong doublehas an 8-byte alignment on x86_64 Linux).Here’s the detailed breakdown:
-
Total size before final padding: After adding up all the fields and necessary paddings between them, we reach an offset of 58 bytes.
-
Alignment requirement: The struct must be aligned to 8 bytes.
-
Calculate final padding:
- Compute the size modulo the alignment:
58 % 8 = 2. - Determine the padding needed to reach the next multiple of 8:
8 - 2 = 6.
- Compute the size modulo the alignment:
Therefore, the final padding required is 6 bytes to make the total size of the struct a multiple of its alignment (64 bytes in total).
Answer: 6 Byte Padding
-