Back to DFS's C Page


OAC Problem Set VII

Dynamic Memory Allocation #1

Filling Structures Using an Array of Pointers

These are the first of several problems which involve the use of dynamically allocated memory. For full marks, you must not only use malloc() to obtain RAM for data storage, you must also ensure that all allocated memory is freed before the program terminates. You must provide hard copy and ask the teacher to run your program.

This problem allows you to become acquainted with dynamic memory allocation in the familiar context of arrays. The array will contain a set of pointers to memory allocated for data structures, each block of RAM being represented by a pointer in the array. This is still not the best programming technique, because the array is statically declared. The next Problem Set will deal with a better method.


Program I: Creating Data, Printing a Table, and Writing to a File

Using the following compiler directive and declarations

#define MAXROLLS 10

struct roll_s {
   int die1,
       die2,
       total; };
struct roll_s *roll_ptrs[MAXROLLS];

write a program named dice1.c which simulates ten rolls of a pair of dice.

Your program must follow the pseudocode provided below.

  1. Fill the roll_ptrs array with NULLs
  2. Create & save data for 10 rolls using a loop
    1. Get memory allocated, saving address in roll_ptrs array
    2. In a function, create & save data into allocated memory, using pointer as an argument
    3. Increment counter
  3. Print out a labeled table of data, using a function call for the printing of each row in the table
  4. Create a file called rolls10 which holds the values of each roll of the dice on a separate line
  5. Free memory used for roll data structures

N.B. For this quickie program, you may assume that the memory requested is in fact allocated.


Program II: Reading from a File, Sorting & Printing a Table

Using the compiler directive and declarations given above along with the one below, cannibalize your source code for Program I and add the following features, saving your file as dice2.c.

struct roll_s *roll_copy_ptrs[MAXROLLS];

Your program will read in pairs of numbers from a file, each of which represents a roll of a pair of dice. After printing a labeled table of the original data, you will use a copy of the array of pointers to sort the data based on the total of each roll. Finally, you will print out a labeled table of the sorted data.

You can adapt any of the following sorting algorithms for your program:

Your program must follow the pseudocode provided below.

  1. Call a function to fill the roll_ptrs array with NULLs
  2. Open a file using a command-line argument
  3. Call a function to read in up to MAXROLLS of roll info using a loop
    1. Get memory allocated, saving pointer in roll_ptrs array
    2. Read roll data into allocated memory
    3. Increment counter
  4. Close file
  5. Call a function which prints out a labeled table of data as was printed in Program I, using the address of roll_ptrs and MAXROLLS as arguments
  6. Use a function to copy the pointers in roll_ptrs into roll_copy_ptrs
  7. Use a function to sort the pointers in roll_copy_ptrs based on ascending order of the totals of the dice rolls
  8. Call the function used in Step 5 above to print out a labeled table of of the sorted data, using the address of roll_copy_ptrs and MAXROLLS as arguments
  9. Call a function to free memory used for the roll data structures

N.B. A graceful exit from the program is required if the user-specified file does not exist. For this program, you must ensure that the memory requested is in fact allocated -- if it is not, you must call the function described in Step 9 before exiting.


Created 20 Feb 01
© DFStermole 2001