4 ms·
Technically you are right. Practically you are right, but the article is more right than you. I got into some fights with quite a few elite teachers/coders tea
by SFJulie 10y ago
Technically you are right.
Practically you are right, but the article is more right than you. I got into some fights with quite a few elite teachers/coders teaching the way of doing 2D arrays exactly as described, and they wouldn't agree the method you describe is less error prone ... because: it is tradition. (And as every person doing research know tradition is not supposed to change and that what science values).
It seems absurd, but so many things are absurd these days I am not even surprised.
- Asooka 10y agoOh, the method I describe is more error prone than array of arrays, because if you ever modify the two variables you used to specify the array size, the compiler will just use the new values for array dimensions. If I had to do array of arrays though, I'd at least do something like template<typename T> T** make_2d_array(size_t m, size_t n) { // TODO: add padding for alignment, // but you're unlikely to need something aligned // on a larger extent than sizeof(void*) void *memory = malloc(m * sizeof(void*) + m * n * sizeof(T)); T** array = (T**)memory; T* data = (T*)((char*)memory + m * sizeof(void*)); for (size_t i = 0; i < m; i++) { array[i] = data + i * n; } return array; } And you can then delete the entire array with a single free().