

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
There are a bunch of complicated rules and heuristics that different compilers use to determine how to lay out your structs. But to oversimplify:
C is a language that aims to give tight control over memory, so the fact that you can control the layout of your structs is a feature, not a bug.
Compilers + modern hardware + optimizations + skill issues means that sometimes what you think the computer is going to do isn't exactly what it actually does. That said, C is designed to get you close to the machine and allows you to dig in and figure out what's going on if you want to for a specific compiler or architecture.
As a rule of thumb, ordering your fields from largest to smallest will help the compiler minimize padding:
typedef struct {
char* a;
double b;
char c;
char d;
long e;
char f;
} poorly_aligned_t;
typedef struct {
double b;
long e;
char* a;
char c;
char d;
char f;
} better_t;
Re-arrange the fields in sneklang_var_t so that the padding is optimal and the tests in main.c pass.