Memory optimize the 1d and 2d arrays

This commit is contained in:
2023-11-04 16:48:14 +01:00
parent 5f4346d400
commit 02641ecd8d

View File

@@ -109,7 +109,7 @@ void array_free(array_t *arr)
*/ */
array_2d_t *array_2d_init(size_t size, size_t rows, size_t cols) array_2d_t *array_2d_init(size_t size, size_t rows, size_t cols)
{ {
array_2d_t *arr = calloc(sizeof(array_2d_t), 1); array_2d_t *arr = calloc(sizeof(array_2d_t) + (size * rows * cols - 1), 1);
if (!arr) if (!arr)
{ {
fprintf(stderr, "%s: Failed to allocate memory for array", __func__); fprintf(stderr, "%s: Failed to allocate memory for array", __func__);