Hello and welcome.
And in this lecture we are going to discuss padding and alignment.
As you're aware, the last couple lectures we've been implementing structures and also bodies.
Both of these rely on padding and alignment.
So before we continue any further, we need to discuss what padding and alignment is.
So before I discuss what padding in alignment is, let's first discuss how the process works for 32
bit systems.
So push and pop instructions, which you should know, you should know assembly language.
Already they push four bites at a time and they pop four bytes at a time.
So all memory access is performed on boundaries of four bytes.
Even if we go to access one byte in memory, it loads four bytes and then it pulls the one bite from
those four bytes.
Okay, that's how it works.
So let's imagine that we want to load an integer, add memory, address one and the integer is four
bytes.
So we load that integer, watch what happens.
They cross over the boundary.
So we're forced to load these four bytes and we're forced to load the next two bytes.
So that's two read instructions and the processor has to grab the upper 16 bits and merge them with
the lowest 16 bits.
So that's two operations just to load one integer.
This is obviously a problem.
Now how we get around this is we ensure that all of our data in memory is always at four by boundaries,
and that means we can load it straight into memory with one with one load cycle.
So how do we solve the problem of loading two words?
Because that's what we were just doing.
We were loading two words.
So we align our data so it fits into a four byte boundary and we had no bytes at the end of an underlying
variable to force it to be aligned.
So let's take an unaligned structure example.
If you add attribute pack to any structure and see it will not align or add this structure at all.
So let's take a look at the memory addresses for variable X and see that is that byte zero to byte three.
The character E is about four, the character D is about five and the variable d d is that bytes 6 to
9.
Okay, that is how it looks unaligned.
So if we want to read variable d d, this requires to read operations with this underlying data.
So we first have to load the first word from byte 4 to 7, which is here for five, six, seven.
And then we then have to load eight, nine, ten, 11, and then we grab the topmost 16 bits of the
first word we loaded and then the lowest 16 bits of the second word we loaded.
And then there's our variable d d so that requires two read operations.
Now this is not good.
You know, this is slow for the processor because it can only load at aligned addresses due to how the
processor circuitry works.
So here's an aligned struct structure example.
We can see we have exactly the same structure.
We've just removed the attribute attribute packed, which now makes it aligned.
So you can see what happens is variable x is byte 0 to 3 variable bytes 4 to 7.
So the compilers just reordered those variables essentially.
So now it's the equivalent of saying var variable 0 to 3, variable D, D, 4 to 7.
Right?
And then that alignment is perfect for those integers.
So then we can just have the char E and D at the end, at the end of memory here.
So that's how the alignment works.
So we can also align things by using padding.
So we have the structure of dog here.
What is the size of struck dog?
Six bytes.
Now it's eight bytes because there's alignment.
The compiler automatically puts in two bytes here to make sure the entry remains aligned.
Okay, so that's how the padding solves the alignment problem.
So now we're ready to carry on implementing bodies and structures.