File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ char *strDup(char *);
22
22
struct nlist * lookup (char * );
23
23
struct nlist * install (char * , char * );
24
24
void undef (char * );
25
+ void freetable (struct nlist * [], int );
25
26
26
27
/* globals */
27
28
static struct nlist * hashtab [HASHSIZE ]; /* pointer table */
@@ -94,6 +95,19 @@ void undef(char *s)
94
95
}
95
96
}
96
97
98
+ /* freetable: free table's (and its content's) allocated memory from heap */
99
+ void freetable (struct nlist * node [], int size )
100
+ {
101
+ int i ;
102
+
103
+ for (i = 0 ; i < size ; i ++ )
104
+ if (node [i ] != NULL ) {
105
+ free (node [i ]-> name );
106
+ free (node [i ]-> defn );
107
+ free (node [i ]);
108
+ }
109
+ }
110
+
97
111
int main (void )
98
112
{
99
113
struct nlist * p ;
@@ -118,5 +132,7 @@ int main(void)
118
132
if (hashtab [i ] != NULL )
119
133
printf ("%i name: %s defn: %s\n" ,
120
134
i , hashtab [i ]-> name , hashtab [i ]-> defn );
135
+
136
+ freetable (hashtab , HASHSIZE ); /* clean up */
121
137
return 0 ;
122
138
}
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ char *strDup(char *);
49
49
unsigned hash (char * );
50
50
struct nlist * lookup (char * );
51
51
struct nlist * install (char * , char * );
52
+ void freetable (struct nlist * [], int );
52
53
53
54
/* binsearch: find word in tab[0]...tab[n - 1] */
54
55
struct key * binsearch (char * word , struct key * tab , int n )
@@ -218,6 +219,19 @@ struct nlist *install(char *name, char *defn)
218
219
return np ;
219
220
}
220
221
222
+ /* freetable: free table's (and its content's) allocated memory from heap */
223
+ void freetable (struct nlist * node [], int size )
224
+ {
225
+ int i ;
226
+
227
+ for (i = 0 ; i < size ; i ++ )
228
+ if (node [i ] != NULL ) {
229
+ free (node [i ]-> name );
230
+ free (node [i ]-> defn );
231
+ free (node [i ]);
232
+ }
233
+ }
234
+
221
235
/* simple define processor (no arguments) */
222
236
int main (void )
223
237
{
@@ -246,5 +260,6 @@ int main (void)
246
260
if (hashtab [i ] != NULL )
247
261
printf ("%i name: %s defn: %s\n" ,
248
262
i , hashtab [i ]-> name , hashtab [i ]-> defn );
263
+ freetable (hashtab , HASHSIZE ); /* clean up */
249
264
return 0 ;
250
265
}
You can’t perform that action at this time.
0 commit comments