Chapter 9
Programming Problems Using Heap
Memory
9.1 Sorting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
9.1.1 Generating Test Input and Expected Output . . . . . . . . . . . . . . . . . . . . . . . . . 125
9.1.2 Redirecting Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
9.1.3 Sorting Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
9.1.4 Using valgrind to Detect Memory Leaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
9.2 Sort Using qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
9.2.1 qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
9.2.2 The Comparison Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
9.2.3 Execution Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
9.2.4 Sorting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
9.1 Sorting an Array
This problem asks you to write a program that reads integers from a file, sorts the
numbers, and writes the sorted numbers out to another file.
9.1.1 Generating Test Input and Expected Output
The previous chapter explained why testing does not guarantee that a program is correct:
There are too many possible cases. Nevertheless, testing still plays a central role in software
development, because testing helps detect problems. Before writing a program, or even a
single function, software developers should always think about how to test it. Often this
involves creating test inputs by hand, based on an understanding of where the weakness in
the function or program may be. This can be a lot of work, however, and it is often useful
to write small programs to generate test case input.
To test sorting, first write a program that generates random integers. To make this
program more flexible, it has one command-line argument: argv[1] tells the program how
many numbers should be generated. The program uses a random number generator. In C,
the function rand returns a number between 0 and the largest integer. These numbers are
“random” because it is difficult to predict which number will appear next. If we call rand
multiple times, it will return an unpredictable sequence of numbers. You can increase the
“randomness” by giving a seed. The seed is used to initialize the random number generator.
If we give the same seed in two different runs of the program, then the program generates
the same sequence of numbers. This can be useful for testing and debugging. If the seed
changes, the sequence also changes. The function srand sets the seed. Linux has a function
time. This function returns the number of seconds since 00:00:00 January 1st 1970. If srand
is called with time as an argument, then the seed changes every second. This is good enough
for many applications that require sequences of random numbers. This is not, however, truly
125