

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Structs
incomplete
2: Initializers
incomplete
3: Scaling Coordinate
incomplete
4: Typedef
incomplete
5: Sizeof
incomplete
6: Struct Padding
incomplete
This lesson's interactive features are locked, please to keep using them
As we saw earlier, sizeof can be used to view the size of a type (for once, programmers thought of a name that was actually helpful). But this isn't just true of builtin types like int or float, you can also use it to find out the size of structs!
printf("Size of coordinate_t: %zu bytes\n", sizeof(coordinate_t));
Structs are stored contiguously in memory one field after another. Take this struct:
typedef struct Coordinate {
int x;
int y;
int z;
} coordinate_t;
Assuming int is 4 bytes, the memory layout for coordinate_t would look like:
typedef struct Human{
char first_initial;
int age;
double height;
} human_t;
Assuming char is 1 byte, int is 4 bytes, and double is 8 bytes, the memory layout for human_t might look like this:
Wait! What is that padding doing here?
It turns out that CPUs don't like accessing data that isn't aligned (incredible oversimplification alert, since obviously CPUs don't have feelings (yet)), so C inserts padding to maintain alignment (e.g. every 4 bytes in this example).
Huge caveat: these layouts can vary depending on the compiler and system architecture.