im working my way through c unleashed and got the section on data organization using arrays. it gives an example program of dynamically allocating memory for the columns as pointers then dynamically allocates memory for each row. so in effect the pointers in each column point to the memory allocated for that row. ie
however he then goes on to say that if the memory allocation fails clean up loops through the number of columns freeing the pointer for the row with free(myarray[i]) but to free the initial call of maloc he just uses free no brackets.Code:myarray = malloc(sizeof(*myarray) * num_columns) for (i = 0; i < num_columns; i++) { myarray[i] = malloc(sizeof(row) * num_elements) }
further down he has a function to clean up after the array is finished with he clears the rows as before but this time he calls free(myarray)
which is correct or do both work. i would try with a simple program (no need to fill the array) but im worried about leaving a memory leak and causing issues.
coop