Message from C, C++ discussions

December 2019

— Everyone

— 

    graph->matrix = (int **)malloc(graph->N * sizeof(int *));
for (int i = 0; i < graph->N; ++i) {
graph->matrix[i] = (int *)malloc(graph->N * sizeof(int));
}

— Hey is that kind of dynamic array allocation fine?

— It's a 2D array

— malloc can potentially fail and return a null pointer

— Why?

— You could run out of memory or there might be no addressable page that fits your size requirements

— Dynamic allocations are not guaranteed to succeed

— Hmm... then what should I do?

— Btw, that graph->N is of type int

— If you need a matrix, why not allocate it at once?
graph->N * graph->N * sizeof(int)

— Oh right