Experiment if Necessary
Before going any further, Gary wanted to get a better understanding of the running profile of programs. He and Graham sat down and wrote some short programs to see how they ran with Graham's custom library linked in. Perhaps they could get a better understanding of the conditions that caused the problem to arise.
Tip
What type of experiments would you run? What would your program(s) look like?
The first test program Gary and Graham wrote (ProgramA) is shown in Example 1-1.
Example 1-1. ProgramA code
int main(int argc, char **argv) {
int i = 0;
for (i = 0; i < 1000000; i++) {
malloc(32);
}
exit (0);
}They ran the program and waited for the results. It took several minutes to finish. Although computers were slower back then, this was clearly unacceptable. When this program finished, there were 32 MB of memory leaks. How would the program run if all of the memory allocations were deallocated? They made a simple modification to create ProgramB, shown in Example 1-2.
Example 1-2. ProgramB code
int main(int argc, char **argv) {
int i = 0;
for (i = 0; i < 1000000; i++) {
void *x = malloc(32);
free(x);
}
exit (0);
}When they compiled and ran ProgramB, it completed in a few seconds. Graham was convinced that the problem was related to the number of memory allocations open when the program ended, but couldn't figure out where the problem occurred. He had searched through his code for several hours and was unable to find any problems. Gary wasn't as convinced as Graham that ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access