arrays in C are friggin weird

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we've all seen arrays before and array is just a block of memory that stores elements that are all the same size continuously in memory and then to access an array we do some kind of expression like this but why do arrays start at zero and also what if I told you that this was also legal in C today we're talking about the math behind arrays and why languages that use one-based indexing Lua are incorrect let's Jump Right In when we're programming and we have variables that are all of the same type and can similar kinds of information we want to store those variables into what is called an array and an array is just a block of memory that is meant to house variables that are the same type and the same size that are contiguous together in memory so to make one we'll make an INT array and we'll call it my array and two denote that it's an array all we have to do is put these square brackets after the name and we can place the size of the array in one of two locations we can either put it here and not preassign values or we can leave this blank and then preassign the values here I can do one 1 2 3 4 for example meaning I have an array now that is of size four and it contains the values 1 2 3 and four and maybe these are user IDs maybe these are lines we read in from a file this could be any kind of information that are all the same type and represent similar kinds of information when we're talking about arrays the first question that comes up for a lot of people is what is the type of this keyword and you're probably thinking well it's an array type no it's not in C the type of an array label is actually a pointer we can prove that by printing it right here so we'll compile the program and then print the array variable and that array variable is going to be hex 7f something something something and we can see that it's a stack based variable because it has this 7f here that 7f means that it's in the stack on a 64-bit system so now the question is how do we use the array right well like I said before to get a value out of an array all we have to do is index into it and to index into it we use the same syntax as the assignment we put the index that we want to use now to get the first element of the array you would think we'd use the number one but here we actually use the number zero which is really interesting because why do we even use zero if I want to get the first element shouldn't I use the number one let's just compile this real quick and show you that the number revealed here is in fact the first element now the reason that this happens actually has to do with the math that happens under the hood when we use the array pointer so like I said before an array is just a block of M memory that contains elements that are of the same size and they're all contiguous right this is like one 2 and three and it goes on until however many elements there are but this is our array in a nutshell the base of this array the keyword we use to describe it is called my array now under the hood when we actually want to access elements in this array the compiler does some math to generate the instructions to get the elements out of it we use the address of the array so my array plus the index you give it and this is in parenthesis here times the size of the elements so the size of int in this case right so what we're doing here is we're actually saying to get an element in my array we do my array plus the index times the size of the integer which is going to be four bytes so when I give it the zero withth index I actually end up doing my array plus 0 which gets us the first element because the my array is is just a pointer what I'm able to do is use basic pointer arithmetic to dreference values inside of it here so by saying the zero with index I'm saying my array plus zero which gets us this element and then the same math applies to get the next element in the array if I want to get the second element I actually going to use one because I'm going to index plus the size of the array to access the memory at that location pretty neat now that's all well and good and I think we all agree that that's what's happening under the hood here but what if I also told you that to get the zero with element of this array we could do this notation here we could say zero of my array now you're probably going to think no surely that won't work right zero isn't a pointer and my aray is a pointer we're using it as an index how how does that all happen under the hood and you can even Pro and we can prove that it works right now by running this command and seeing that we still get did I actually save this and seeing that we still get the piece of information that we did before so how how is this happening how is it possible that we can use zero as an array an index off by the my array pointer you may be thinking that it works the exact same way you're thinking oh well surely the transitive property of addition means that a plus b is the same as b + a and while this holds true in math it actually falls apart in programming because I have my array which in the previous example is equal to zero and the index is equal to the pointer value of my array multiplying that by four the size of an integer would make that array index overflow and none of this math would work out we would get some random address it' be some value mod 64 bits and then it would just crash we wouldn't get to the location that we need to do it's much simpler than this and it ends up being a bit of an optimization that the compiler does so in the event that I have the array of an index or an index of the array in both cases all this turns into is the thing pointed to the dfference value of a + I where a is a pointer type and I is a integer type now if I were to flip these if I made a the index and I the base at the end of the day internally one of these is still a pointer what's happening internal in the compiler is it's taking the value that is the integer type the non-p pointer type and it is upgrading it to the index so it's multiplying it times 4 and in this case it's multiplying still 0 * 4 because Zer is that index value so all we have left is I which is our my array value What's Happening Here is not so much of a transitive property of addition thing it's just the compiler deciding which value gets upgraded to the pointer in which value does not so at the end of the day both of these are legal but not for the reason you probably thought so this is why I very much dislike languages like Lua I'm thinking about what's happening under the hood when I see an array where you index into the first element to get the first element I'm I'm thinking in the back of my head well then where is that first element there where is the zeroeth element now if you learned anything here you'll probably learn something too about why void pointers exist they're a typeless pointer that actually has a lot of power in the C language go check that out right there see you guys there
Info
Channel: Low Level Learning
Views: 102,234
Rating: undefined out of 5
Keywords: header files, header files in c, why do header files exist, c programming, linker, compiler
Id: kdgq-OwsOs8
Channel Id: undefined
Length: 6min 56sec (416 seconds)
Published: Thu Jan 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.