Hello and welcome.
In this lecture, we're going to implement some functions which will allow us to get the size of a data
type.
So if we just go to compilers and we search for struct data type.
You will see we have many different sizes.
We have the size of the actual day type element.
We have the size of the data type array.
And if it's a pointer, then the size should obviously be four bytes.
So we obviously need some functions to handle this for us correctly.
Okay.
So let's head the data type C and we're going to create a function called type size which will return
the data type size.
So we're going to say is we're going to say size T data type, size, struct data type, D type, and
we're just going to say if the D type has the is pointer flag and the D type.
Point, a depth is above zero, then return data size D-word.
So essentially if this data type is a pointer and it has a point of depth above zero, so what is a
pointer depth?
Well, look, a struct type here or if we got there one ask.
Yes, that's a point depth one.
If we had two asterisks, I'd be appointed depth of two.
So if the data type is a pointer and the point depth is above zero, then it must be a pointer.
So we return date sized word which is four bytes.
Next, we need to check if it's an array
and if it's an array then we're going to return D type array dot size.
So we return the array size.
Now if it's not a pointer and it's not an array, then we return the D type size, which is for single
elements.
So that's all good.
So now we're going to create a new data type size function.
So we're going to go size T data type size, no PR, no pointer, basically struct data type, D type.
So all this is going to do is we're going to check D type flags, ampersand, data type flag is array.
And if that's true, we're going to return D type array that size.
Otherwise we're going to just return the D type size.
So this function will ignore the fact that it might be a pointer and it's going to return you the size
as if it was never a pointer, even if you pass it one.
So if you pass it a char name pointer by here, it will return a size of one, not four if they call
this function.
So obviously we plan to use all of these functions later on.
And rather than having to keep going back to type C to write a new function, we have this lecture just
to do it all at once now.
So now we're going to say size t data type size for array access.
So now we're going to actually get the size for array access.
So what we're now going to do is we're going to say if data type is struct or union D type and the D
type flags is a pointer and the D type pointer depth is one.
What do we do?
While we return the D type size.
And the reason for that is because we have something like struct ABC, asterisk ABC and then we basically
have A-B-C zero, which would obviously convert it into something that isn't a pointer, you understand?
So that's why we check if the point of depth is one and the data type is a pointer and it's a struct
or union.
Because remember, this is for array access.
We we only call this function one where using arrays more will be discussed on it later on when we do
actually use it.
And if it's not a struct or union pointer, we're just going to return data type, underscore size D
type, which will then call our size data type size function down here, which will return the date
type size.
Okay, nice.
So, so we're now going to say size tw data type element, size struct data type D, type F D type flags
and percent is pointer.
Then we're going to return data size D word OC.
So this function data type element size will only return the actual element size on a per element basis.
So if you pass an array data type here, it will only return the the most smallest part of that array.
So for example, if we pass a array of char, then the size that will be returned will be one, not
maybe 20 bytes, for example, if it's an array of 20 chars.
So once again, if we do something like Char ABC 50 and we pass this ABC data type to this function,
it will return one OC because it's more focused on what the data type is, not the array size.
So this is what this function is doing, guys, right?
So then just under here, we're going to say return D type size.
Okay, that's perfect.
The last thing to do now is to copy and paste all of these prototypes.
And we're going to go to compile that.
We're going to search for data type which can scroll down so we can find the data type functions.
There it is.
I'm just going to paste it in.
And now we need to obviously get rid of all of these bodies so that only the prototypes are available.
And yeah, these functions will allow us to break down our data type sizes as is very important part
of the system.
So let's just check a compiles.
We're going to say clear, make, clean, make.
And yeah we can see that compiles just fine.
If you're seeing in declaration areas and stuff just ignore it for now.
That's from the previous lecture where we started implementing the bodies because we haven't yet finished
implementing the bodies yet.
This is just a mini lecture to get us some of the stuff we need before we can continue.
So yeah.
Congratulations.
You now have functions for calculating data type sizes.