Efficient use of dynamic memory - C and realloc
Archive - Originally posted on "The Horse's Mouth" - 2010-03-10 18:18:23 - Graham EllisC is basically a language in which you dimension your arrays at compile time - however, at run time you can call up malloc or calloc to grab a chunk of dynamic memory. This is very useful if you don't know how much space you'll need at compile time. We've several example in our memory management module on our C training courses.
But sometimes, you'll only gradually learn how much memory you need, even at run time. Take, for example, a simple example where you want to read a file into memory and then be able to go through it, many times, line by line. You won't know how many lines there are at the beginning ... so a single malloc or calloc won't do. But there are solutions.
First, you could use realloc to extend the size of the memory block you have each time you add a new line.
Second, you could set up all your lines as linked lists, where each one contains a line of data and the address in memory of where the next line may be found.
The First option may lead to "thrashing" as memory is allocated and reallocated while the file is being read, with a large number of movements of the data between heap areas. And the second option may also be slow when you come to find data that's well down the list.
There is a Third Way. You can use realloc, but extend the size of the memory blocks in much more substantial chunks, and that's something that we did on today's course. The full source code may be found here. The extra lines / critical lines are:
need = counter + 1;
if (have < need) {
info = realloc(info,(counter+16) * sizeof(char *));
have = counter+16;
}
Basically saying "if you haven't got enough space for another line, then grab space for 16 more ...