I'm having a big troubles figuring out how to rightfully create a 2D dynamic array, how to assert the memory and how to free it in the end.
I'll show you parts of my code, and please tell me what I'm doing wrong.
I declare on the dynamic array in main function and send it to BuildMatrix function that is supposed to assert the needed memory to the array and fill it.
that's how I declared on the array and send it to the function Build:
int row, column, i, j;
int **matrix;
BuildMatrix(&matrix, row, column);
now thats BuildMatrix decleration:
void BuildMatrix(int*** matrix, int row, int column);
And that's how I assert the memory (row and column have values that the user chose)
matrix =malloc(row * sizeof(int *));
for (i = 0; i < row; i++)
matrix[i] =malloc(column * sizeof(int));
Now so far everything works just fine, but when I try to free the memory, I get break point error
That's the function I used for freeing the memory:
void ExitAndFree(int** matrix, int row) {
int i;
for (i = 0; i < row; i++) {
free(matrix[i]);
}
free(matrix);
}
The debugger show me that the error is on the first free (if I remove the first one, the second gives the error)
There is also another problem, but I think its to much for now, I'll ask later... :P
Thanks for your help!!
P.S: If you got any good tutorials about pointers and dynamic arrays (I'd rather 2D+ arrays) I'd appreciate it a lot.