calloc() & malloc() | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is cinemas so in the last session we discussed some mal log function in debt dynamic memory allocation concept so now in this session we will see how a catalog function will execute and how real log function will execute so already we discussed catalog function is used to allocate the memory dynamically to arrays right c kellogg memory allocation memory allocation to arrays arrays so what is the prototype of a Kellog function here can log function is taking two arguments so first one size underscore t n second one size underscore t size two arguments it is taking what is the size underscore t it is unsigned value and sign so what is this n sizeof array size of array and what is this size size of each element in the array so first one is a size of array second one is a size of each element in the array two things and here it is a catalog function return type is a wide pointer just like a malloc function right so what catalog function will do means a two arguments we have to pass what is the size of array and the size of each element we need to pass on success suppose if sufficient memory is available then catalog function allocates n into size bytes of memory block it allocates a memory block so that is n into size bytes and it returns the base address on success on success returns address of block memory block address of memory block it returns for any reason if it has failure on failure it returns null pointer it returns null point so this is a success case and this is a failure case write a Cal log function returns the base address of the block on success on failure if no memory to allocate then it returns a null pointer okay now we will see right how it executes using a program so how a Cal log function allocate the memory so we'll see programmatically in the main so first we are declaring so one variable e n N and here it is we are asking printf enter sizeof array enter sizeof array scanf s we are reading the size into n we are reading the size into n and here it is next we need to allocate the memory to a pointer variables only so we are declaring one pointer variable that is a our our array variable so here it is we are calling Cal log function it is taking two arguments what is the first argument so first argument is a sizeof array N and what is the second argument size of each element so what type of elements we are storing into array array is of type integer so sizeof integer we are advancing directly we cannot pass two because integer size varies from compiler to compiler so it is better to based on size of function so it will return so catalog function and what catalog function will do just consider n value is a 5 n value is a 5 consider a RR gets memory allocation it is a pointer variable it holds address so first catalog function returns I mean first allocate 5 locations just consider integer occupies a 2 bytes memory 2 bytes the base address is a 2 0 4 6 next 1 4 8 Phi 0 Phi 2 Phi 4 so total 10 bytes memory it will allocate a 10 bytes means n into size of integer n is a 5 sizeof integer is a 2 so 10 bytes memory it will allocate first after allocating 10 bytes memory it will return to 0 for 6 but problem is it returns in the form of wide pointer because the return type of array is a wide pointer sir why it returns void pointer already we discussed in a mail log function discussion right why the reason using Cal log function - what type of array you will allocate the memory the Dennis Ritchie team cannot understand sometimes it may be integer type array it may be a character type it may be a float type double type or any other type of character array user-defined also chances are there so by the time it always try to return a common type of all what is the common pointer of all what is the generic pointer of all is a wide pointer so catalog function return type is a why'd pointer okay but that we are collecting into what type of pointer what type of array you declared type conversion is important pointer casting is important so here a catalog function is returning a pointer that is a wide pointer type but we are collecting into ARR ARR is a integer pointer type so here it is a type conversion is required type conversion after typecasting so we are collecting the catalog function return value that is 2 0 4 6 we are collecting here and it is pointing and remember once you allocate the memory dynamically using catalog function right all these locations initializes with zeros all these locations if memory is available if memory is available all are initializes with a 0 now as soon as memory has been allocated now I want to print all the elements I just want to check all the locations are initialized with the zeros or not I just want to check how can we check only just print and check that is only the option okay so first of all if a ARR equals to null so when it will return null on failure if memory is not available then it returns a null value so by that time directly we are printing no memory to allocate no memory message we are printing clearly no memory for example in else case in else now I want to print all the elements printf printf array elements are printf array elements are how to print the elements I am writing the logic for loop for loop high value starts with a zero I is less than four I equals to zero I is less than n I plus plus because so total we have n elements nothing but five elements so according to our example so five times we are repeating and here we are printing printf printf percent is be sir how to print here we have to use a pointer or thematic concept how AR our value is a two zero four six so I value varies from zero to five so for example 0 1 2 3 4 index consider index we are printing like this a RR + I point this is a logic how it executes sir a our value is always 2 0 4 600 - 0 4 6 - 0 4 6 and so on I value 0 1 2 3 is a pointer pointer pointer now observe - 0 4 6 + 0 is a 2 0 4 6 pointer 2 - 0 4 6 means the value which is inside the location 0 it will print next - 0 4 6 plus 1 pointer or thematic concept nothing but whenever we increase the value of your pointer by 1 it is pointing to the next location of array nothing but how many bytes it will increase it will increase by size bytes suppose here it is integer integer size is a 2 bytes so pointer also will be increased by 2 bytes not one bite so 2 0 4 6 plus 1 is nothing but 2 0 4 8 - 0 4 8 2 pointer next - 0 4 6 plus 2 means plus 1 plus 2 so it returns 0 so in this way it will print all the elements of that array that is so this is how a catalog function allocate the memory and after allocating the memory what are the values of a memory block what are the values of memory block and finally how can we process the information of that blog so catalog function the catalog function is available in a std and ly bit dot H library so whenever we are working with these functions we need to include that library we need to include that library this is how the memory we are allocating two arrays using a Cal log function okay so in the next session we will see so what are the disadvantages of a catalog function so why they introduce the concept of a real log function real log function is also used to increase and decrease the size of a an array why can't we decrease and increase the size of array using Cal log function that is only the drawback that we will see in a next session for more videos log in to narrate a channel thank you you
Info
Channel: Naresh i Technologies
Views: 339,077
Rating: undefined out of 5
Keywords: C Language, Srinivas, Naresh IT, Hands on C Language Training, C Language Demo, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions
Id: pTXvkLlAm38
Channel Id: undefined
Length: 12min 44sec (764 seconds)
Published: Thu Sep 22 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.