I want to create a 2D array of ints, where the array's dimension is given at run time (not compile time). So far, I have been using:
int **ptr = malloc(m * sizeof(int*)); for (size_t i = 0; i < m; i++) ptr[i] = malloc(n*sizeof(int));
However I have read that this is not a good solution (waste of memory, can lead to memory leak, not having a real 2d array with all elements adjacent in memory, and other problems).
So, my question is, what is the correct way of creating a 2D array where its dimensions arent known at compile time?