Hello, long time no questions, and the site was down yesterday My question will be simple today, as always haha. Hmm, I have the follow code:
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
void *pToData[100] = {NULL}; //pointer to all the data's to be free( )'ed
static void addToList( void *data ) {
static int i = 0;
pToData[i++] = data;
}
static int *createArray( void ) {
int *array = malloc(SIZE * sizeof( int ) );
int i;
//check to see if we gain the memmory
if( array != NULL ) {
for( i=0; i<SIZE; i++ )
array[i] = i;
}
else {
printf( "Could not create array!\n" );
exit( 1 );
}
addToList(array);
return array;
}
static printArr( int *arr, char *label ) {
int i;
puts( label );
for( i=0; i<SIZE; i++ )
printf( "%d ", arr[i] );
}
static void freeAll( void ) {
int i;
for( i=0; pToData[i] != NULL; i++ )
free( pToData[i] );
}
int main( void ) {
int *arr1, *arr2;
arr1 = createArray();
arr2 = createArray();
printArr( arr1, "Array 1: " );
printArr( arr2, "\n\nArray 2:" );
freeAll(); //is this really works? so easy?
return 0;
}
As you can imagine, I'm trying to make a function that will free all the memmory I allocated by using the function createArray(); Is this really free's the memmory? If so, I'll rewrite the malloc function with some help, and then make a part that always if I malloc'ed some memmory it calls addToList() and then at the end of the program I don't need to free() every single thing, I just call freeAll and it free's for me, of course, if this works