| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
/* Use C's dynamic memory allocation functions. */ |
| 8 |
|
| 9 |
/* Invoke the program using one command-line argument specifying the |
| 10 |
size of an array. This array consists of pointers to (possibly) |
| 11 |
allocated arrays. |
| 12 |
|
| 13 |
When the programming is running, select among the following |
| 14 |
commands: |
| 15 |
|
| 16 |
o allocate memory: a <index> <memory-size> |
| 17 |
o deallocate memory: d <index> |
| 18 |
o read from memory: r <index> <position-within-allocation> |
| 19 |
o write to memory: w <index> <position-within-allocation> |
| 20 |
o quit: q |
| 21 |
|
| 22 |
The user is respond for obeying (or disobeying the rules on dynamic |
| 23 |
memory use. */ |
| 24 |
|
| 25 |
#ifdef MTRACE |
| 26 |
#include <mcheck.h> |
| 27 |
#endif /* MTRACE */ |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <assert.h> |
| 31 |
|
| 32 |
/* Allocate memory with the specified size, returning nonzero upon |
| 33 |
success. */ |
| 34 |
|
| 35 |
void allocate (char** array, size_t size) |
| 36 |
{ |
| 37 |
*array = malloc (size); |
| 38 |
} |
| 39 |
|
| 40 |
/* Deallocate memory. */ |
| 41 |
|
| 42 |
void deallocate (char** array) |
| 43 |
{ |
| 44 |
free ((void*) *array); |
| 45 |
} |
| 46 |
|
| 47 |
/* Read from a position in memory. */ |
| 48 |
|
| 49 |
void read_from_memory (char* array, int position) |
| 50 |
{ |
| 51 |
volatile char character = array[position]; |
| 52 |
} |
| 53 |
|
| 54 |
/* Write to a position in memory. */ |
| 55 |
|
| 56 |
void write_to_memory (char* array, int position) |
| 57 |
{ |
| 58 |
array[position] = 'a'; |
| 59 |
} |
| 60 |
|
| 61 |
int main (int argc, char* argv[]) |
| 62 |
{ |
| 63 |
char** array; |
| 64 |
unsigned array_size; |
| 65 |
char command[32]; |
| 66 |
unsigned array_index; |
| 67 |
char command_letter; |
| 68 |
int size_or_position; |
| 69 |
int error = 0; |
| 70 |
|
| 71 |
#ifdef MTRACE |
| 72 |
mtrace (); |
| 73 |
#endif /* MTRACE */ |
| 74 |
|
| 75 |
if (argc != 2) { |
| 76 |
fprintf (stderr, "%s: array-size\n", argv[0]); |
| 77 |
return 1; |
| 78 |
} |
| 79 |
|
| 80 |
array_size = strtoul (argv[1], 0, 0); |
| 81 |
array = (char **) calloc (array_size, sizeof (char *)); |
| 82 |
assert (array != 0); |
| 83 |
|
| 84 |
/* Follow the user's commands. */ |
| 85 |
while (!error) { |
| 86 |
printf ("Please enter a command: "); |
| 87 |
command_letter = getchar (); |
| 88 |
assert (command_letter != EOF); |
| 89 |
switch (command_letter) { |
| 90 |
|
| 91 |
case 'a': |
| 92 |
fgets (command, sizeof (command), stdin); |
| 93 |
if (sscanf (command, "%u %i", &array_index, &size_or_position) == 2 |
| 94 |
&& array_index < array_size) |
| 95 |
allocate (&(array[array_index]), size_or_position); |
| 96 |
else |
| 97 |
error = 1; |
| 98 |
break; |
| 99 |
|
| 100 |
case 'd': |
| 101 |
fgets (command, sizeof (command), stdin); |
| 102 |
if (sscanf (command, "%u", &array_index) == 1 |
| 103 |
&& array_index < array_size) |
| 104 |
deallocate (&(array[array_index])); |
| 105 |
else |
| 106 |
error = 1; |
| 107 |
break; |
| 108 |
|
| 109 |
case 'r': |
| 110 |
fgets (command, sizeof (command), stdin); |
| 111 |
if (sscanf (command, "%u %i", &array_index, &size_or_position) == 2 |
| 112 |
&& array_index < array_size) |
| 113 |
read_from_memory (array[array_index], size_or_position); |
| 114 |
else |
| 115 |
error = 1; |
| 116 |
break; |
| 117 |
|
| 118 |
case 'w': |
| 119 |
fgets (command, sizeof (command), stdin); |
| 120 |
if (sscanf (command, "%u %i", &array_index, &size_or_position) == 2 |
| 121 |
&& array_index < array_size) |
| 122 |
write_to_memory (array[array_index], size_or_position); |
| 123 |
else |
| 124 |
error = 1; |
| 125 |
break; |
| 126 |
|
| 127 |
case 'q': |
| 128 |
free ((void *) array); |
| 129 |
return 0; |
| 130 |
|
| 131 |
default: |
| 132 |
error = 1; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
free ((void *) array); |
| 137 |
return 1; |
| 138 |
} |