So in the last lecture we briefly implemented array brackets for variable declarations and we left out
the calculating size because I wanted to kind of explain to you a little more how we calculate an array
size.
So we're going to explain that in this presentation today.
So firstly, what is an array declaration?
Well, here are some examples.
You can see there variables that are that are arrays.
So in it x 50, so we have 50 integer elements in this variable X and then in a Y we have two dimensional
array variable here that has 30 left indexes and 20 right indexes.
And then we have a three dimensional array here that has 100, 203 hundred.
So all this means is it means we have 100 of 200 arrays of 300 characters.
So this last, last array bracket here represents the actual char.
So how do we calculate array sizes?
Because that's why we left it.
That's why we left it out.
Last lecture.
So I could explain this to you a little more.
Well, let's take it x 50.
So we first get the size of the data type, so size of in it, and then we multiply it by the array
index.
So that's 50.
So the actual expression is four multiplied by 50 because integer is four bytes in size.
Let's take a look at this two dimensional array.
So again, we do size of the data type which gives us four and we multiply it by 30, which is the first
array index and then we multiply by 20, which is the last array index.
Let's take a look at the actual three dimensional arrays.
So once again, we do the size of char, which gives us one and we do one, multiply by 40, multiply
by ten, multiply by 20.
And that's how we calculate the array sizes.
We basically multiply the data type size, whether it be a structure or primitive type.
We will apply the data type size by every index.
And then once we've gone through all the indexes, we have the the absolute size of that variable.
And we should also note that it's unaligned at this point, one, using this math.
So later in the course, I'll explain to you how you calculate offset for arrays, which is a little
a little different.
But we won't explain that here because we haven't even started creating statements yet.
So I hope that makes sense about how we calculate array sizes.
So what are the rules for calculating an array size?
We can only calculate array sizes at compile time.
The constant indexes that are constant are literal numbers.
So what are the rules for calculating arrays sizes?
We can only calculate array sizes at compile time for constant indexes that are constant or literal
numbers.
So if we go back, this works because 30 will always be 30.
Okay?
If you provided, let's say X in there or I don't know, ABC where the 30 is and ABC does not represent
a constant number, then we can't compile that at compile time.
Some runtime code would need to be present to actually calculate what is going on there.
So especially for global variables, it's absolutely mandatory that the numbers are constant.
So I hope that makes sense.
So as we go through the course now, we're going to start implementing those calculation of sizes in
the arrays and then we'll move on to continuing making our variables in our parser.
Thanks for watching.