Transcript for:
C realloc Usage and Behavior

In this presentation, we will try to understand how to dynamically allocate memory using realloc function. So, let's get started. What is realloc by the way? realloc function is used to change the size of the memory block without losing the old data. It's a special type of function obviously which is used to change the size of the memory block without losing the old data. It is a built-in function again declared in std live.h and as the name itself suggests that realloc which means reallocation Obviously, it means that it is changing the size of the memory block. Now, the syntax is quite clear. Here you can clearly see that obviously it will return a void pointer. Apart from this, it requires two arguments. The first argument is the pointer to the previously allocated memory. It needs the pointer to the previously allocated memory and obviously it also needs the new size. Now, on failure, realloc returns null. Similar to the other functions, realloc also returns null. Let's consider one example here. Let's say we have a malloc function which allocates memory for one integer. Okay. Here you can see size of integer has been specified here which means it will allocate let's say four bytes because we are assuming that size of integer is four bytes. So malloc will allocate four bytes in heap. Let's say somewhere in my code I reallocate the memory. In this case obviously the pointer to the previously allocated memory is required and second argument is the new size. Here in this case I want one more integer to be allocated. That is why I have to consider the previously allocated memory as well. Here in this is I should specify 2 into size of integers. So, the total memory which is available with me is of 8 bytes. So, previously we have 4 bytes, now we have 8 bytes. This particular function will return an address and that will get stored within this PTR pointer. This is quite an interesting function, isn't that sir? Let's consider some points related to it. This will allocate memory space of 2 into size of integer which is equivalent to 8 bytes because we are assuming size of integer is 4 bytes, right? Also, this function moves the contents of the old block to a new block. and the data of the old block is not lost, let me tell you. This is interesting because this function is actually allocating new memory but it is also taking care that old data should not be lost. So, this function moves the contents of the old block to a new block and the data of the old block is not lost. We may lose the data when the new size is smaller than the old size. Okay, so here you can see the new size is greater than the old size. So, the data will not be lost but when the new size is smaller than the old size, then the data may get lost. Apart from this, newly allocated bytes are uninitialized. The bytes which are allocated newly by this realloc function will not get initialized. We should initialize them. That should be very clear. Now, let's consider one programming example in order to cement this concept. Here you can see that we have a malloc function. Obviously, this malloc function is allocating 8 bytes of memory. You can see over here, this is 2 into size of integer. Again the assumption is, size of integer is 4 bytes, so total bytes allocated by this malloc is 8 bytes on the heap memory. This malloc function will also return the address of the first byte of the memory which will get stored within this ptr pointer. We should check this also if ptr is equal to null, then memory is not available and we should exit from the program with exit failure status. Otherwise, we can continue. Here in this case, we are asking the user to enter the two numbers. Now with the help of realloc function, what we are trying to do here is, We are trying to allocate memory for two more integers. Here you can see the size specified over here is four into size of integer. Here we should note this very carefully that we should keep the count of the previously allocated memory as well. Okay, that is why it has to be four into size of integer. We want two more integers. Previously we have the memory for two integers. Now we want two more integers. That's why I have to specify four into size of integer. Obviously, here in this case, PTR has to be passed. Which means the pointer to the previously allocated memory. Realloc function will allocate memory for two more integers. If it is the case that ptr is null, then again we should print this out on the screen that memory is not available. Else, we will ask the user to enter two more integers and we will store those integers within this newly allocated memory. You can see the for loop starts from i equal to two, let me tell you, instead of starting it from zero. After this, we have the step that is printing the values on the screen. As simple as that. We will print all the values entered by the user and that's all. Let's execute this program. So, let's enter the two numbers. Let's enter two more integers. Let's say this is 79 and this is 90. You can clearly see over here that all the four integers get printed on the screen. This is how real log function works. Okay friends, this is it for now. Thank you for watching this presentation.