In this video, we're going to put
together some of the concepts we've seen in this course,
as well as previous courses in the specialization,
into a slightly larger example.
We're going to use getline and realloc to read lines from a file,
increasing the size of the array that holds the lines each time we read one,
so that we can store them all in memory at once.
Once we have them all read in,
we're going to sort them, and then we're going to print out the sorted result.
This sort function is not something from the C library.
We've written it ourselves.
It uses Q-sort which you saw in the previous course.
We're not going to show the details,
but it will rearrange pointers in an array such that
the strings they point to are sorted with string compare.
Now, we're going to step through the code.
We declare the variable lines and initialize it to NULL.
We declare the variable current and initialize it to NULL.
We declare the variable size without initializing it.
We could if we wanted to,
but this will illustrate the getline will give it an appropriate value.
And we initialize i to zero.
And then we call getline.
We set up a frame and enter the C library.
Getline determines that current is NULL.
So, it allocates memory and sets current to be a pointer to that memory.
Then it changes size to be the number of bytes allocated.
Next, it reads from the stream past ant,
which in this case, the standard ant,
so it reads the string cat followed by a new line
and a null terminator, and returns to main.
The call succeeded, so we enter the while loop.
We're going to call realloc on lines,
which is NULL, passing in zero plus one,
which is one, times the size of *lines,
so we need space for one char*.
Reallocing NULL is just like mallocing.
So, we're going to get one box which is uninitialized.
Lines at i equals current.
So, we're going to give the zeroth box of lines a pointer to the string cat.
Then we set current equal to NULL because when we call getline again,
we don't want it to change this string,
we want it to allocate memory for a new string.
We increment i and return to the start of the while loop.
Now, rather than stepping into getline again,
we're just going to show its effects this time.
That is, it's going to allocate memory for a string,
change size to be the number of bytes allocated,
read in the string and from standard in,
this time we showed it allocating a different amount of space,
only five bytes, which is exactly what it needs.
We don't actually know how much space it'll allocate.
We just know that any space left over will be uninitialized.
In fact, how much space it allocates will be platform dependent.
We enter the while loop and realloc lines to be two char*s now.
So we create a new two-element array,
copy in the value of the previous array,
leaving the second box uninitialized,
and get an arrow to the newly allocated memory.
Lines at one is going to get the value of current which is a pointer to the string ant.
We set current to NULL so we will get a new allocation next time we call getline,
increment i, and go around again.
We call getline again to read the last line in the stream.
We realloc our array and update lines at two to be current.
We set current to NULL, increment i,
and go around the loop again.
Now, when we call getline,
it is at the end of file so we skip the while loop.
Notice that it still allocated some memory which is uninitialized,
so we need to free that memory.
We're going to call sort on the lines.
However, we're not going to step through it.
We're just going to know that it reorders the pointers
so that ant comes before bread which comes before cat.
Now, we're going to enter the loop that prints each string,
so we print lines at zero,
which is ant, and then we free that memory.
Then we print lines at one which is bread, and free that memory.
We print lines at two,
which is cat, and free that memory.
Now, we're done with this for loop.
Next, we're going to free lines,
the memory pointed to by the blue arrow.
So, that's going to go away.
Notice that we've cleaned up our heap nicely and freed all of that memory.
Now, we're going to return destroying main's frame.