0
0
Embedded Cprogramming~10 mins

Struct packing and alignment in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a packed struct in C.

Embedded C
struct __attribute__(([1])) PackedData {
    char a;
    int b;
};
Drag options to blanks, or click blank then click option'
Aaligned
Bunused
Cdeprecated
Dpacked
Attempts:
3 left
💡 Hint
Common Mistakes
Using __attribute__((aligned)) instead of packed
Forgetting to use attribute at all
2fill in blank
medium

Complete the code to specify 1-byte alignment for the struct.

Embedded C
struct Data {
    char a;
    int b;
} __attribute__(([1](1)));
Drag options to blanks, or click blank then click option'
Apacked
Baligned
Cdeprecated
Dunused
Attempts:
3 left
💡 Hint
Common Mistakes
Using packed instead of aligned
Not specifying the alignment value
3fill in blank
hard

Fix the error in the struct declaration to correctly pack the struct.

Embedded C
struct Data {
    char a;
    int b;
} __attribute__(([1]));
Drag options to blanks, or click blank then click option'
Apacked
Balign
Calignas
Dpack
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'align' or 'pack' which are invalid
Confusing with C++11 alignas keyword
4fill in blank
hard

Fill both blanks to create a struct with 2-byte alignment and packed attribute.

Embedded C
struct Data {
    char a;
    short b;
} __attribute__(([1])) __attribute__(([2](2)));
Drag options to blanks, or click blank then click option'
Apacked
Baligned
Cdeprecated
Dunused
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the attributes
Using deprecated or unused attributes
5fill in blank
hard

Fill all three blanks to define a packed struct with 4-byte alignment and a member with 2-byte alignment.

Embedded C
struct Data {
    char a;
    short b __attribute__(([1](2)));
} __attribute__(([2])) __attribute__(([3](4)));
Drag options to blanks, or click blank then click option'
Aaligned
Bpacked
Cdeprecated
Dunused
Attempts:
3 left
💡 Hint
Common Mistakes
Using packed on member instead of aligned
Misordering attributes