- We're going to wrap our discussion on pointers,
by learning yet one more very powerful feature
related to managing memory and the function malloc.
Let's recap the relationship between pointers and arrays.
Static arrays waste memory, because you have
to allocate, for example, in this case 20 positions
just in case you might need them later.
When you declare an int array 20, that memory block,
whether you use it or not, is being set aside
for this array, and nothing else.
No other program, no other function
within your own program can use it.
There is a better way to declare arrays using pointers.
The function malloc, which is short for memory allocation,
will set aside also initially 20 times the size of int,
which is essentially the same thing that we did
in the line above with an int array.
Except now, we are simply pointing the array
named "pointer array" with a star in front of it,
to point to a location that could
potentially be used by up to 20 integers.
We don't have to use all of it.
When we are done working with the array,
we are going to call another function, called "free array".
This is a library function built into the C language,
that in conjunction with the operating system,
it frees up any unused memory of the array
to be returned to the pool of memory, or the memory heap,
so that other applications can use it.
Let's look at the code and let's get
some more insight into this.
Just for illustration purposes, I declared
an array called "int static array".
That array is not used in this code,
it's just there as a reminder that once you declare
that static array of size 20, 20 int,
it doesn't even matter if you use it,
it is being allocated, and the memory now has been wasted.
The better way to do that would be in line 21,
is to allocate an int which I call "dynamic array",
using the same malloc function of 20 time the size of int.
This means that I need an array of size 20,
pretty much the same one as the static one,
except now you will see that this is
a much more efficient way.
How do I store something in this array,
even though I didn't call it an array?
Line 22, I store at the beginning
of the array, the value 10.
Please recall that from a previous video,
we showed that a pointer to an int
can be assigned the value of the beginning of an array.
In other words, if the pointer can point
to the beginning of an array, I don't need
the array itself because I am just going
to manage memory directly using the pointer.
I am assigning now the number 10,
or the value 10, at wherever dynamic array points to,
and de facto, this is now the beginning of an array.
We are going to print the value
and convince ourselves, "does this work?"
Line 25, notice that I am treating dynamic array
as if it was already declared as an array, which it hasn't.
Only static array in line 17 has been declared
in the traditional way of an array.
Line 25 dynamic array, there is no such declaration.
This is again a further illustration that arrays
and pointers are essentially the same notation.
I'm going to assign the value 20
to position one in the array.
Let's print this, and see what happens.
Address, 75c038a0 stores value 10.
This comes from this line right here, line 23.
Because, I have just assigned 10
to the beginning of what I referred to as an array.
Then in line 25, I'm placing 20 at position one,
and I am using the same type of print out
as if it was an already pre-declared array.
Finally, I'm calculating the size of
this dynamic array, and it's eight,
because each one of the cells of the array,
or the elements of the array, is an int,
which is four bytes, a total of eight bytes.
Now, recall that I have allocated 20 ints
for this array, but I only used two.
Therefore, the command in line 30,
or the function free dynamic array
returns to the memory heap 18 positions,
that are unused, since I only used two of them.
This is a much better way of using an array.
It allows later on a pointer to continue
to point to different elements and add them
to the array, thus using only the amount
of memory needed without the waste associated
with pre-assigning a static block
without any knowledge whether
it will be used in its entirety or not.