61 - DYNAMIC MEMORY ALLOCATION - C PROGRAMMING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our channel so in the previous sessions we have covered many concepts in the C language and today we discuss about one important topic in C language that is dynamic memory allocation dynamic memory allocation that means allocating the memories for the variables at one time so here the allocation will be done at hand right so don't be confused so I will clarify by just meant by this one thing so we do an example so let us take an eye this concept so if you concern an address concept so first we have to declare the added so we have to specify the size by the declaration so leave it with the address in some some marks something so this implies so the marks is an array of integer datatype and it can hold a maximum of ten elements right so this is the maximum size so why I am saying it says micron size means so we may use the ten elements and we may not use the tenant so here there are two cases so case one so if we are using only finding means if you are using only five elements so the remaining five elements size is this so we are not using the remaining final news we are using only the five elements so the many five element size will be pasting that means sorry memory memory memory allocated for five images history so for example in occupies some two bytes of memory if it is a 16-bit compiler that is fancier it tells the 16-bit come by that set out to base two words so counter memory occupied by this marks array is n elements 20 pipes of memory will be allocated for this end and here we are using ordinate and bytes of memory and what about the remaining 10 bytes of memory will be hasty right so this is one piece so we are wasting the memory so without declaring the size without giving the size this a declaration is invalid so in the declaration part itself we have to do the maximum size this is a contract so what about the case to so 10 elements are there so using 20 bytes and if I need to add 3 more elements to be added here four elements to be added so this cannot be possibly right so even though the air is having the limitation on boundaries but so these are the two pieces we can face by giving them size at the compile time so this is called a compile time allocation allocating the memory in comparable so far and what is such time we can allocate the memory so at all time so we did not go with the arrest acceleration so we get the whole thing whatever the required number if we need so that memory can be added allocated C so for this allocation hope you understood this one friend so this is called a comparative allocation so declaration part means compiled evaluation now so for this allocating this memory in during the one time right so for this allocated memory allocation for allocating the memory or anything we are having and we love function CLR function so for allocating the no way we're going to functions tell function is for increasing the memory increasing demand so that is really we you know reallocation and for DL kidding but so as we are creating allocating the memory during the runtime so the user is responsible to be allocate the memory after its use so unless the user D allocates the memory the memory will not be edged so if you have reading the memory using one among these three functions it is required to free that function even free that memory ok so whatever the memory we are allocating that memory should be D allocated by using this sweet function okay teri will see one by one what is the syntax of for this ml of talent and as a rock so ml art means it's a memory allocation which and stands for memory allocation whatever the function we are using for allocating the memory that will be in a pointer type okay it's a wide pointer it returns a void pointer so that we can take us to our required data right so the syntax for this one is pointer is equal to I cast a malloc function since right so this is nesting next one I'm a log function so if we want to allocate the memory see example I will give you the example so the business so point then I want to store the pointer right so pointer integer so in star I am typecast into each stand and below so I need the integer values to be stored into the memory so that's one I'm simply giving some n in to size of in so we are having one function called size of which gives the size of the technical so this will be allocating see here if L is equal to funny if M is equal to 5 then size of integer size of integer means if it is a 16-bit compiler it is a 2 bytes of memory so 5 min to 2 that means 10 points so 10 bytes of memory will be on pupil or 10 rights of memory will be allocated to this point and this point that we'll be addressing being however suit so it's a boy pointing actually this pointer is a white pointer so we have type-cast to the required format so then malloc function and we have to specify the size so here in the face so if we want to store some 5 variables or 6 values so that that will be mentioned here and in to size of India so if it is a float it can be any size of throat so that 4 bytes of memory will be occupied for each value in a 16-bit compiler so 4 into 5 for our fund device of memory will be allocated in this point and see this will return in so tiny bytes of memory and this point there will be only thing towards the starting edges the beginning address okay so for each and every value we have to implement the pointer so that so it is an integer so 2 bytes of memory will be occupied for each and every value so this is how we can use this ml or function similarly serum so you see the complete my sub memory will be occupied or allocated to the pointer here see see our function see a lot function 0 function means contiguous memory and ten years from America allegation so here the memory will be located in terms of blocks in terms of drugs so that face so see I use this index so same index here also it will fill the void pointer so that we have to typecast to the dacoit format so for the purpose PT our pointer sum is equal to point available point variable 0 I do some in star I am giving the integer pointer so CLO and here we have to mention the size but we were to make sure the two parameters that is a number of groups and the size the number of blocks in size how many number of plots we need so example so let us take the previous example we need to store the from five variables right so five values so I am giving PTR is equal to sum in this term so you can also go with the fraudster okay so see you know and I am giving some fine blocks and the size is size of size of e okay so size up in here like five blocks will be related each of two ways if it is a 16-bit number right so two bytes of memory the point that again it will point here self or indirect a point the starting of the edges so hope you understood so if you increment the pointer automatically next it will be moving to the two words with a difference of combines because it is a integer similarly if you want to use a phone so we can find the PTR is equal to some fruits so typecasting to the pointing that way hello some file come on sighs Oh fruit so this you can use so if you use this one the same fire box will be dedicated and each of four words each of four words or you envelope function complete if we do them in the indigent okay so we can write here PDF is equal to in time and my log file size of flow so if this one then a complete block of memory of 20 bytes will be allocated and the pointer will be addressing either party is the point that we'll be addressing to starting address of this memory okay so here it will create it blocks the number of bronze so only the differences here we function will give only one parameter and CLR function will be two parameters so these two are used to allocate memory and we look and see so maybe these will be used and there is a one more function we have seen that we have our function so if the memory allocated with the help of these two ma lot or CLO is not sufficient and if you want to add a good number of memory then we will be visiting enough that means a reallocation really delegation that means we can increase the memory existing memory which is created with the help of this ml okay see LM function see the syntax for this one is simple pd r is equal to real long we have to increase the ponytail with y so how much we need to increase that should be written here so this pointer will be increased PTR is equal to sum' so I am allocating some of the body see so this segment will allocate some five into through size 10 bytes of movie and if I want to increase the mode number of I mean if I want to increase the memory so then I believe the real log P here is equal to real work some PT a-- or simply we can go with a number so that it will be inclusive to M less go into science so 2 to 14 bytes of memory will vacate it so one thing you have to remember the reallocation will be turned if we need to increase the memory which is already have accreted by using these four functions so if you are if you are allocating the memory using the compile time and if you are using this it will not be successful right so if you are if you want to increase the memory which are allocated using one among these two that means I fell apart 0 then this real our function will be closed right so as I said that these are the I mean these memory is allocated by the user the memory is allocated by the user at the runtime so it is necessary to release the memory or be irrigate the memory by the user itself so for that purpose we are having four D allocation deallocating the memory we are having a free function so unless you use this free function the memory allocated by using these functions will not be released so the Sigma X for this one is a simple free of point so whatever the pointer we are using during their vacation that pointer should be given as an argument for this function so that whatever the memory we are using we are allocating using Emma log or CLR or this is relevant that memory will be get lives in if you lose this function and once again it is the responsibility of the user to release the memory after using them so that that fight will use this function so this dynamic memory allocation will be having with the four functions one is M alone for allocating C ever for allocating memory in terms of blocks and the real work in order to increase the memory and a free function in order to reduce the Boqueria or D allocate them right so these are the functions we are using in this dynamic memory allocation and these will be used in link produced simply we can say in data structures we can use this dynamic memory allocations in order to save the memory so there is more wastage of memory and there is more chance of getting out of boarding sections right so hope you understood gets so let us stop here and if you are having any doubts regarding this is a bank with memory allocation so feel free to post your roles in the comment section so then definitely I will try to clarify all your thumbs and if you really understood my sessions like my session share messages with your friends and don't forget to subscribe to our Channel so thanks for watching thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 28,948
Rating: 4.9418993 out of 5
Keywords: sundeep, saradhi, kanthety, c language, c programming, cp, computer, computer programming, loops, Indirection Operator, De-referencing Operator, Value Operator, Address Operator, Pointer, pointer declaration, introduction to pointers, & operator, * operator, unary operators, computer language training, c language training, void pointer, null pointer, datatype, variable name, dynamic memory, malloc, calloc, realloc, free, memory allocation, runtime memory, contiguous memory
Id: y6oeSYfRW8Q
Channel Id: undefined
Length: 19min 56sec (1196 seconds)
Published: Sun May 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.