int** matriz = initMatriz(rows, cols);
From that point on, how do I find out the number of rows and the number of columns?
Well, the answer is right there. Just look at the parameters rows and cols and they will give you the answer.
Okay, maybe you want to know how to do this if you no longer have access to the values of rows and cols. And the answer is that you can’t do that, at least not in a portable way. The value of the pointer is a number that tells on which memory address begins the block of memory that has been allocated, but says nothing about its size.
If you know the deep details of your implementation of malloc, maybe you can find this information in the memory allocation tables that are created or some other type of internal information maintained by malloc. However, a solution based on this is inherently nonportable.
The solution adopted in almost all cases includes you encapsulating the pointer to the array and the size of the array allocated together in the same structure for this. This way, you eliminate the fundamental problem that is not having the size of the area allocated along with that area itself. For example:
typedef struct {
int rows;
int cols;
int** pointer;
} Matriz;
Matriz* initMatriz(int rows, int cols) {
int i, j;
// Aloca a memória para todas as linhas.
int **matriz = (int**) malloc(rows * sizeof(int*));
for (i = 0; i < rows; i++) {
// Para cada linha, eu aloco o número de colunas.
matriz[i] = (int*) malloc(cols * sizeof(int));
// Inicializa.
for (j = 0; j < cols; j++) {
matriz[i][j] = 0;
}
}
Matriz *resultado = (Matriz *) malloc(sizeof(Matriz));
resultado->rows = rows;
resultado->cols = cols;
resultado->pointer = matriz;
return resultado;
}
To destroy an array in order to free up memory:
void freeMatriz(Matriz *matriz) {
for (int i = 0; i < matriz->rows; i++) {
free(matriz->pointer[i]);
}
free(matriz->pointer);
free(matriz);
}
So Victor appreciated the answer, I was using this structure already, but before I got to use it I had this doubt really, because I found it interesting and I could not solve it in any way. For normal initiated vectors, it is possible to find the number of positions. through the sizeof(vector)/sizeof(int) code, I thought q would work, but when dynamically starting this code no longer serves. But thanks to your explanation you helped me a lot to understand.
– Skywalker
Only one doubt, the memory release process, I can give the free only in the matrix?
free(matriz)or I need to freepointerand then in the matrix?– Skywalker
@Skywalker To use the
free, the process is simple (although find information explaining this may not be): Everything you received frommalloc, you must return withfree. If themallocgave you a pointerx, then you must return that same pointer withfreein the future. You should not pass tofreesomething you haven’t received from amalloc. You shouldn’t call thefreemore than once to some pointer received frommalloc. You should not forget to call thefreefor something received frommalloc.– Victor Stafusa
Um yes, so I need to give a free for all the columns in a for and then yes to the Pointer, and then to the matrix. right?
– Skywalker
@Skywalker I mean, first you give a
free(matriz->pointer)and thenfree(matriz). I edited the answer to show this.– Victor Stafusa
perfect, thanks @Victor Stafusa
– Skywalker