0:02
Now, let's take a look at the semantics of realloc.
You can think of realloc as mallocing new space,
copying the old data to the new space and freeing the old space.
Let's see that in action.
We start by declaring a pointer and mallocing space for 10 ints for it to point at.
Next, we are going to fill these ints with some data.
In the interest of space,
we have abstracted that out into a function called initValues which is not shown here.
For this example, we don't really care what the values
are but we want you to see that they do get copied.
So let's initialize this data with some values.
Now that we have numbers in the array,
let us suppose that we discover we need to make the array larger.
Such a thing might happen if you were reading
data and did not know how much data you needed to hold.
In this example, we decide that we need to hold
14 ints instead of just 10 so we call realloc.
We pass in P which points at the memory we
want to reallocate and the amount of space we want.
In this case, space for 14 ints.
Now, let's see what realloc would do.
First, it mallocs space for the newly requested size,
14 ints, so let's do that.
Next, realloc copys the data from the old memory into the newly allocated space.
Notice that it only copied 10 elements.
Since that is how many were in the old array.
The remaining four are uninitialized.
Last, realloc is going to free the original space which is no longer needed.
Once that is done, realloc will return a pointer to the newly allocated memory.
Finishing the assignment statement,
we'll update P to point at this newly allocated memory
since the function call expression evaluates to reallocs return value.
Now, let us suppose we were to do
some more initialization to fill in these other four values.
Again, this code is not pictured in the interest of space.
You can also use realloc to reduce the amount of memory allocated to something.
You might want to do this if you no longer need some of
the data and want to reduce the memory that you are using.
This will follow the same rules.
Let's see it in action.
First, you malloc space for four ints.
Then, you copy the first four ints into the new space.
Finally , you free the memory that was allocated for the old space.
As before, realloc returns a pointer to the start of the newly allocated memory.
So after realloc returns,
the assignment statement updates P to point at this newly allocated memory.
You should always assume that realloc will move the data to
a new location both when you write code and when you execute code by hand.
However, you should be aware that realloc may leave the data in the same place.
Let's see the same example if realloc keeps the data in the same memory location.
We start out in the same way with a call to malloc and then initializing our data.
Next, we call realloc to increase the size to 14.
However, this time, realloc is going to
allocate space immediately after the current data,
avoiding the need to copy and free the old data.
As before, realloc returns a pointer to the new data.
This time, it just happens to be in the same place as before.
Our assignment statement will then update P to point at exactly the same place as before.
That is perfectly fine.
Next, we initialize these four new values
and then call realloc again to reduce the size to four.
Realloc can choose to just free the unneeded elements at the end,
reducing the size of this allocation to the newly requested size.
It will then return a pointer to this smaller array and as before,
P will be updated to point at the same place.
Note that whether or not realloc keeps the data in
the same place or moves it around depends on
the particular implementation of realloc as well as which
memory addresses are allocated and which are free when realloc is called.
You cannot guess if it will leave the data in place so you
must always write code that expects realloc to move the data.
If it leaves it in place,
that code will work fine.