How to use dynamically allocated arrays

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in today's video we'll talk about how you can create your own dynamically allocated array but before that we're going to take a look at a set of questions that will give you different and better solutions to your possible problem right so first things first what's a dynamically allocated or a dynamically allocated array is something that can be accessed in any function and the allocation the allocation process is controlled by us the programmer right so usually you want that dynamically allocated array to be used in many places inside your project now if you need that if you need that I usually suggest that you use a statically allocated array that is also global so what you can do here is instead for example if let's say we want an int array of 256 numbers you can just say int an array of 256 and that's going to allocate 256 integrals on to the global memory and we can all course use this or you can say array of final ten equals seventeen and then printf the result so for Sandy back such an array of ten and there you go the cool thing about global arrays is that they are automatically instantiated or initialized with the value 0 so you don't have to mention it anywhere inside the project right nor inside the main main function or inside your constructor each function ok so now that we got this over this is the first question right if you want do you need an array used in many other places not just in the function that is declared right so if you need that I suggest you just use a global variable secondly if you need an array of variable size do you know the maximum amount you can store inside that array right the maximum amount of integrals can store if so you might be able to actually say actually declare it right off the bat with the maximum amount of of elements inside that array so I say here our maximum is 256 right we know we cannot ever store more than 256 elements inside all right we don't need more than that if if that's the case I suggest you use this but only if that maximum amount of elements is not too high right so it doesn't consume too much memory and is known so do you know do you know the maximum amount of elements right and is that is that max amount too big right so that you don't waste memory like if I had an array in which the maximum amount of integrals was I don't know 64,000 elements but in most cases you won't get anywhere near a thousand to two thousand then I think it's safe to say you might need a dynamically allocated array now if you want on a raid it is accessible from anywhere it's also it can also store many elements and you want to optimize for that amount of elements so sometimes it's going to store 100ml sometimes gonna store 6000 elements right if you want to optimize for that then I suggest you use a dynamically allocated array so in here instead of doing this if you actually answered yes to the third question right you should be using an dynamically allocated array and how to use it well let's get over that right now so to start with we cannot actually use the array declaration notation here because these are automatically allocates 256 integrals on the global memory so what we have to do is instead get a pointer to the start of the memory that we want to allocate to do this we're just going to remove this and just say int asterisk already right so this is a pointer it's not really a dynamically allocated array it is just a pointer so we have to point it to dynamically allocated direct to do this well to do this we need a place where your instantiate in all the variables right so a sort of main function start function whatever that is that initializes or your all your actual variable so to initialize this array what you have to do is say array equals and here we can use CL log and then this guy takes in basically first is the number of elements so how many you want initially at least so I know for example we can say 256 and then the size of each element that is well size of integral because that's what we're storing inside our function our array so this will initialize 256 integrals but now since it's dynamically allocated and you control your allocation of that memory you also need to deallocate it once you finished using it otherwise the system doesn't really know when you're done with it so at the end of the program just say three and our array right before we didn't have to because it was global memory and global memory is still in a sort of way static to the program so it's going to be automatically allocated at the end of the program in this case it's not so we have to free this memory and from that point you can actually use the array as a simple array you can see here array of 10 equals 17 and then and then just print it on the screen so if I run this it's going to work because while this now behaves like an array now one thing that I suggest you do instead of just using an array you also have to store the size of that array somewhere because if you want to reallocate it if you want to change its size you can't have to store it somewhere it doesn't store it on its own and you cannot really get size of that array in C so what you have to do is say for example size T I know array size let's say and that should be some number right like 256 for example as a baseline and then you can actually use it here in CL op right this is not dynamically allocated it's allocated a global memory you don't have to really dedicate it it's automatically be allocated by the program so we run this this is going to be exactly the same right we have 70 and that's fine and dandy but but one more thing we said in the beginning that this array is of variable size so at one point we're gonna have to change its size so how do we do that how do we actually for example double the amount of the elements inside our array well that's very simple to do first we have to change our array size because that's where we store it and we should actually have an amplitude reference to it let's say here array size so we want to double it whatever that is so x equals 2 because that's going to just double our array size and then what we have to do is use the real log function and relock what takes as parameters is first the pointer to the dynamically allocated whatever memory so array in this case and then the second parameter is the new size of that array so man your size is well our array size but it's in bytes so we have to multiply that by the amount of bytes inside our our integral right so this usually is 4 bytes but we never know but just calling real log it's not enough because well this guy also reallocates memory right so it may move it because well it might not have enough space to just expand it you will have to move it somewhere else well enough space there so that means that you actually have to change the value of the array that's where the return value of the real log comes in so you have to actually reassign the previous pointers and say array equals via log and that's going to return the new the new location in memory for the dynamically allocated memory it might be the same but it might not right so you should actually use it like this all right and now if we try to use it right we still get 17 and even if we actually print F after reallocation it's going to work the same way because the memory is being copied over now one more thing before you go this is basically it in terms of educating dynamically allocated arrays and while changing their size and also freeing that memory but you should also check for nulls if you ever get a null value here for example Cielo can return a null value write a new pointer and when that happens well your program is going to crash and that's not that great right so I think you should actually just check if array equals no and just print up an error for example that let me do F prime F to the standard error and just say array not allocated no allocated for example and just return here one for example has an error code and do the same thing when reallocating because also we a lot can return a null because you might not have physical you might not have enough space or the memory might be actually fragmented so even though you have like gigabytes upon gigabytes of RAM your memory might be used like for example half a gigabyte here then 256 Meg's here and so on and you want a full gigabyte and nowhere is there a full chunk of one gigabyte available for the memory allocator so then the allocation functions such as C a log and real log actually return null right so you should actually check for null here and going to array not reallocated one last thing I forgot to explain is that I used C a lock here instead of Emma log because C a lock aside from having used to actually pass in both the number of elements and its size it automatically sets all that memory to zero so if you usually with arrays you want that but if you don't want that use Emma log and that's also fine it's going to be a bit faster so that's about it with dynamically allocated arrays right think about these questions the use cases if you really really really need it and if you do use it as such check for null it's important to check for null do you see a lock if possible and reallocate properly your array thank you guys so much for watching if you've got any questions leave them down in the comments below or on our discord server and well see you guys next time take care bye
Info
Channel: CodeVault
Views: 18,192
Rating: 4.921875 out of 5
Keywords: c how to make dynamic array, how to create a dynamic array, c dynamic array, dynamically allocated, dynamically allocated array, malloc, c malloc calloc, c calloc realloc, c realloc codevault, dynamic array
Id: 6Ir4l0VuI7Y
Channel Id: undefined
Length: 11min 29sec (689 seconds)
Published: Fri Mar 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.