Hello and welcome.
In this video session, let's discuss a few examples related to dynamic memory allocations that we have
discussed in previous video sessions.
In this example, we are going to discuss the usage of malloc.
Here we included the required header file.
And we have taken a pointer to an integer and assigned it to null initially.
And we have taken a count of ten.
In this case we are going to allocate dynamic memory using malloc.
Meanwhile let's see the syntax of malloc using man page.
So in the syntax of malloc it takes size underscore t and returns one star.
Now let's say we want memory of ten integers.
Then we have to mention size of integer into ten.
This gives dynamically allocated memory in the heap for ten integers.
Since malloc returns void star that is pointer to void.
We need to typecast this void star into the required data type that is, integer pointer in our case,
and assign it to integer pointer variable.
Now if malloc fails, we get p int equal to null and we print the message and exit the program.
If malloc is success, we print the success result and then we assign required values to the dynamically
allocated memory and then print the value that we have assigned.
And finally, once the memory allocation is been utilized we have to free the memory.
So during free we have to pass the pointer that is obtained by dynamic memory allocation.
In this case p int points to the dynamically allocated memory.
And we need to free the same using free.
Let's compile this program and run it to see the result.
Malloc dot c is the name of the file.
Let's compile it.
GCC malloc dot c hyphen o malloc.
Here we have created a object file called as malloc.
Let's run this.
Here we are getting malloc has successfully allocated memory.
And we have assigned some values.
And we are displaying some values using printf function.
So in this case we have assigned the integer 0 to 9 with the count 0 to 9.
And we are printing the same using the next for loop.
We have obtained the expected result here.
Now in the next example, let's discuss Calloc in Calloc.
Also, we need to include the header files that is required for malloc and in this case we are going
to take a structure named user and we have type define it to user as shown in the code.
Before using Calloc, let's open the manual pages of Calloc.
In case of Calloc, we need to pass two parameters.
One is name and next is the size.
So here we take a pointer variable to this user structure and assign it to null.
Initially let's say we want ten such structures allocated dynamically.
So we use calloc.
In this case calloc of ten numbers followed by size of one user structure can now dynamically allocates
memory on heap and returns ten such structures.
The memory location returned returns a void star.
Here we need to typecast it to the required data type.
In this case, we have to typecast the return of Kellogg to user.
Star, which is nothing but the pointer to this structure and we are storing in.
Pointer variable P user.
If Kellogg fails, we get the pointer variable p user.
Equal to null.
And we print the same and exit the code.
In case of Kellogg, all the bytes that is obtained is assigned to zero internally.
But in case of malloc it does not assign the allocated memory to zero.
So here we print when Kellogg is success.
And then we loop ten times here and we store the ID and salary.
Let's say we have salary and ID as a data elements of the structure.
And we store some random values as shown in the loop.
After that, we again print back the id and salary of the structure from dynamically allocated memory
using the loop.
Finally, we free the pointer variable p user using free.
Let's compile this program now.
GCC Kellogg dot c hyphen o Kellogg.
Now let's run this.
We get the success message.
Here we have ten structures.
And for each structure we are assigning the incremental ID starting from zero and salary in increment
of one.
So we have ID of zero has salary equals to 100.
And ID of one has salary equals to 101 and so on.
So this is what we have assigned here.
And we are getting the same using one more for loop And finally we have freed the dynamically allocated
memory.
One important point is the difference between malloc and calloc is in malloc.
A single pointer is taken.
Single parameter is taken, whereas in Kellogg it has two parameters.
One is the size of the data type, and other is how many such data type is required.
Now let's go to the next example and discuss Realloc.
Let's see its syntax first.
In Realloc, whenever we get the memory allocation used during malloc or calloc, this pointer is passed
here and the new size is being specified in realloc.
So the realloc function re-allocates the already allocated memory and returns the pointer.
This return pointer may be same or different from the original pointer.
The same we had discussed in previous video session also.
Now let's see the program.
Here we include the required header files.
Let's say that initially we had performed a malloc of ten integers and assign it to p where p int is
a pointer to integer.
And in the same way, if p is equal to null, we say malloc has failed and we exit with malloc success.
We are again assigning some value to the newly allocated memory.
Now let's say at some point of time we want to increase the allocated memory without changing the older
values stored here.
In this case we use Realloc and we pass the same pointer that we have obtained during malloc.
And then let's assume we want a new memory of size 15 integers.
So initially we had ten integers.
Now we need 15 integers using realloc.
We can resize the allocate memory.
We can resize the allocated memory in the same way.
If realloc returns null.
Then we say Realloc has failed and we exit the program.
If Realloc is success, we get the reallocated memory.
Now, in a loop of 0 to 15, we are assigning some values and then we are displaying the same.
Let's compile this program.
GCC realloc dot c I o Realloc.
Now let's run this.
If you see here initially we had ten bytes of dynamically allocated memory.
Now here we have 15 bytes of dynamically allocated memory.
Now let's say that we want to alter only the newly obtained memory that we obtained using Realloc that
is, from 10th memory to the 15th Stint memory.
Previously we had allocated from 0 to 9.
So now let's provide values from 10 to 15 and then see the final results.
Let's see the result again.
I am recompiling the code again.
Let's run the program.
So if you see here this is the old allocated memory values got by malloc.
And these values are not altered.
But after realloc we have assigned only the new allocated memory.
But the old value is unchanged.
As a summary we have until now discussed malloc calloc realloc and free.
These are the function calls built using BRK and sbrk system calls and dynamic memory is allocated on
heap Thank you for joining this session.