Dynamically allocated multi-dimensional arrays in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in threes video I want to take a look at how to dynamically allocate a multi-dimensional array so we know that a multi-dimensional array statically allocated looks something like this you know all right we have an array that is of size 3 by 3 and then what's happening here we have basically a one big array that holds all the other three small arrays and each of those arrays are have free integrals in them there's one crucial thing to realize in all this that in this arrange multi-dimensional array there's no pointers involved right if I try to print out its size so if I say let's say print F % ll you backslash N and have your size of array if I run this program you'll see I get 36 and 36 is really if we divide by the side of int which is 4 we're gonna get 9 9 integrals that's exactly how many we have here you notice there's no extra space occupied by my pointers so if you try to visualize the way the memory is allocated for a statically allocated array it's really a just a very big blob right just a square and this is the whole array suppose and inside of it there is actually three more arrays right so we have one array here and then another array here and then another array here and inside each of the array there is an integral right so another box let's say and well this is how the array is in memory and suppose that these are continuous places in memory are not in a 2d plane because well addresses are just a number so here we would have let me actually get a bigger font for it that's probably good enough so this is 1 and here we have 2 and then 3 and then 1 two two three and one and two and three here come on here at the end so in reality we only have a box that contains everything there is for it and when we try to get its let's say the address to that array we simply actually just get another to one of these boxes whether it is in an inner box or it is the outside box they are all contained inside of each other but when we dynamically allocated we cannot really have this structure I only made part of this video that you can check up top and at the end I was talking about how you can dynamically allocate a multi-dimensional array so to start off you would have to first have a double pointer if we were to implement it in such a way that we can index it with two indices okay so let's just comment this out and if we do that and we start with a point of double pointer so int double pointer array then we know that this is already defined and it's been allocated so there's space for this address not where it points to but for this address for this pointer so you're gonna have to draw a box for it in the visualization of it here and we simply just draw a simple box here right and with this double pointer what we want is well whenever we say array of zero for example we want to get the first in our array this guy let me say array of two we want to get this guy right and when we add zero at the end we want of course the first element inside the third in our array okay so let's first tackle the first problem we want basically a real zero and array of one of two to give us another array that means that whatever we're pointing to has to be on array so in here this guy really points to an array we have here a simple arrow this represents the pointer and actually make it a bit or obvious okay this is an arrow that should point to an array and we denoted an array by just marking with a rectangle here this would be our array what are the elements of the array well the element of the array since we are we want them themselves to be able to do to get the first second and third element on them using this second index they are also going to be pointers so in effect we're going to have the first pointer here the second pointer here and the third pointer here and they themselves are going to point to another array in which we actually hold the numbers so in here what we can do is just drag an arrow with a very straight one but it's narrow nonetheless that points to a to an array I'm gonna actually have it on the vertical axis but that doesn't really matter and here we can have three boxes which represent the actual numbers that we want to store in this in this set up so in the end we're going to have something like this so this original pointer points to an array of three pointers in here pointers are basically just boxes with an arrow at the end and each pointer points to another array but now this array points or has integrals now let's try to actually implement this in our code okay so we started with array here and we should allocate space for it we just say Emma log of well we want what do we want here we want integral pointers and we just want three of them you can change this number to whatever you want it's not set in stone and now next up what we want is to actually allocate space for each so so at each point points to a dynamically allocated place in memory to do this we have to iterate over each pointers now I have a for loop let's actually define here I I equal 0 I plus plus all right and then here I'm gonna have array of I this time so array of I is each of these boxes you know they don't point to anything they just have a an arrow that points to nothing so we have to change that to point to a dynamically allocated place in memory which is Emma log of size of int not in pointer we allocate an array of integrals not of pointers and right now we just have three integrals in that right we have 1 2 & 3 again you can change this as well it doesn't have to be the same as this guy that's your choice okay so this is how you can create this structure in memory and that is dynamically allocated you cannot really replicate this structure that we have with arrays unless you have just one single array and you don't index it with two indices okay so in this case this is the only way to do it for dynamic allocation of course to actually set each element we are gonna have to manually do this with array of 0 and an array of 0 so the first element in the first list is going to be 1 and the first the second element in the first stitch is going to be 2 and you get the gist of it so on and so forth we can probably do it with for loop or usually if you need these types of arrays they are pretty big they don't fit inside the stack memory so you'd want to have them dynamically allocated and you're gonna automatically initialize them all right just one key difference so as I said the structure is different but also the space the memory it takes is different because as you can see in this overview we don't only have numbers right in this overview we only have numbers we only literally have numbers well they are grouped they have their in boxes but there are just numbers no pointers but in here we actually have pointers besides numbers so what's the size of this multi-dimensional array well first we have the numbers which we know it's 4 times 9 because there are nine integrals and each integral is 4 bytes that would be 36 and then we have well first we have this pointer here which is plus 8 usual it is 8 bytes if you're on a 32-bit machine is just going to be 4 bytes that's one of the main differences between 32 and 64 bits at the pointers are larger so here we have 8 bytes and for each point are inside this array we also have 8 bytes so that's going to be plus 3 times 8 bytes and as you can see that's more than 36 it's actually sixty eight bytes in total so this is much bigger than 36 bytes and well this is only for 2d arrays right here we have basically for every single line we have eight extra bytes right so for this line we have one but one pointer which is eight bytes and for this one as well for this one as well and at the end of the day we also have another one out here but that's sort of for any array so if you have a one-dimensional array it's just eight extra bytes just for that one point that we need to store somewhere but for this to Larry is going to be 8 plus 8 times however many sub arrays you need in that level and of course for 3d arrays each of these numbers remember they're going to be a pointer right so this multiplies so I'm going to have to it's going to be more and more costly to actually have more dimensions to your erased your dynamically allocated arrays okay so now we we still have this code that says print off the size of the array if I try to launch this we will get the size that we calculated we're not really we are gonna get eight that it is just the size of this double pointer so that's sort of to be expected but then how do you get the size of that array well you can't really get the size of the array other than actual calculations like we did here so you could actually calculate size of the array and then iterate over the first level of the arrays and then add on all the pointers but then you're gonna have to say something like three times size of int there's gonna be again it's gonna be something this plus size of array of zero times three that's for the pointers and then plus size of pin times three times three so that would be the formula for calculating its size if we try to run this you'll notice I'm gonna get 68 that's the expected result and of course to free this this array what you simply do is the opposite of what we did what we did up top so instead of first allocating the array and then it's sub arrays you first D allocate the sub arrays so you say not array of Phi equals that but free of array of I and also don't forget to set it to null once you read it because it's easier than to debug of course after you're done with that you just save tree operate and array equals not something like this if you do it backwards well there's an issue because you see if you do it like this well you freed the array but then you're trying to dereference it here and that's just gonna probably give you an error or if your luck is going to work but sometimes it's just not going to work and you're not going to know why so it is important to just do it in the opposite order in which you allocated them so first they allocate the subarrays and then the allocate the outer arrays and this applies to any sized array not just to the arrays and that's really all there is to it two multi-dimensional and that's really all there is to dynamically allocated multi-dimensional arrays and that's that's a mouthful I hope you got something out of it if you do have any questions please leave them down comments below or on our discord server and I do recommend that you actually watch the other video multi-dimensional arrays where I take a look at another way of storing multi-dimensional arrays it doesn't actually have you create pointers to pointers to pointers to pointers you just have one pointer and then you index it in such a way that it's sort of works almost the same as as multi-dimensional array and it's also more memory efficient all right take care bye you
Info
Channel: CodeVault
Views: 14,180
Rating: 4.983871 out of 5
Keywords: array, malloc, 2d, multi, dimensional, dynamically, allocate, c (programming language), codevault
Id: -y8FUvRq_88
Channel Id: undefined
Length: 14min 17sec (857 seconds)
Published: Fri May 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.