Building a function using Dynamic memory allocation

Viewed 37
#include <stdio.h>
#include <stdlib.h> 

void addition(int column, int row) {
    int **array_a = (int **)malloc(sizeof(int*) * column);
    int **array_b = (int **)malloc(sizeof(int*) * column);
    int **array_c = (int **)malloc(sizeof(int*) * column);

    for (int i = 0; i < column; i++) {
        array_a[i] = (int *)malloc(sizeof(int) * row);
    }

    for (int i = 0; i < column; i++) {
        for (int k = 0; k < row; k++) {
            array_a[i][k] = rand();
        }

        for (int i = 0; i < column;) {
            array_b[i] = (int *)malloc(sizeof(int) * row);
        }

        for (int i = 0; i < column; i++) {
            for (int k = 0; k < row; k++) {
                array_b[i][k] = rand();
            }
        }

        for (int i = 0; i < column;) {
            array_c[i] = (int *)malloc(sizeof(int) * row);
        }

        for (int i = 0; i < column; i++) {
            for (int k = 0; k < row; k++) {
                array_c[i][k] = array_a[i][k] + array_b[i][k];
            }
        }

        for (int i = 0; i < column; i++) {
            for (int k = 0; k < row; k++) {
                printf("%d ", array_c[i][k]);
            }

            free(array_a);
            free(array_b);
            free(array_c);
        }
    }
}

int main(void) {
    int column, row;
    scanf("%d" "%d", &column, &row);
    addition(column, row);

    return 0;
}

I'm building a function that creates two 2D arrays and fills the values with random numbers and combines their values. There is no error but the printf isn't working. Any way to make it work? I don't know how to explain in more details but if you ask me I'll answer.

1 Answers

There are multiple problems in your code:

  • the loops are not properly nested: the body of the second for (int i = 0; i < column; i++) loop should stop right after the nested for for (int k = 0; k < row; k++) loop.

  • there are missing i++ increments in some of the loops: for (int i = 0; i < column;) should be

    for (int i = 0; i < column; i++)
    

    This is the main issue as it causes an infinite loop, repeatedly initializing array_b[0] with a newly allocated block, until memory is exhausted and beyond.

  • you do not check for memory allocation failure.

  • your use or columns and rows is odd: the first index in a 2D usually refers to rows and the second to columns, but since your usage is consistent, it is probably not a problem.

  • you do not output a newline after each row or column: all output is produced on a single line without a trailing newline. This may cause your output to not appear on some systems. You might want to pause the program after the output to let the user see the output.

  • casting the return value of malloc() is not necessary in C.

  • you do not free the subarrays.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>

int **allocate_array(int column, int row) {
    int **array = malloc(sizeof(array[0]) * column);

    if (array == NULL)
        return NULL;
    for (int i = 0; i < column; i++) {
        array_a[i] = malloc(sizeof(array[i][0]) * row);
        if (array_a[i] == NULL) {
            while (i-- > 0) {
                free(array[i]);
            }
            free(array);
            return NULL;
        }
    }
    return array;
}

void free_array(int **array, int column, int row) {
    if (array) {
        for (int i = 0; i < column; i++) {
            free(array[i]);
        }
        free(array);
    }
}

void addition(int column, int row) {
    int **array_a = allocate_array(column, row);
    int **array_b = allocate_array(column, row);
    int **array_c = allocate_array(column, row);

    if (array_a && array_b && array_c) {

        for (int i = 0; i < column; i++) {
            for (int k = 0; k < row; k++) {
                array_a[i][k] = rand();
            }
        }
    
        for (int i = 0; i < column; i++) {
            for (int k = 0; k < row; k++) {
                array_b[i][k] = rand();
            }
        }
    
        for (int i = 0; i < column; i++) {
            for (int k = 0; k < row; k++) {
                array_c[i][k] = array_a[i][k] + array_b[i][k];
            }
        }
    
        for (int i = 0; i < column; i++) {
            for (int k = 0; k < row; k++) {
                printf("%d ", array_c[i][k]);
            }
            printf("\n");
        }
    }
    free_array(array_a, column, row);
    free_array(array_b, column, row);
    free_array(array_c, column, row);
}

int main(void) {
    int column, row;
    if (scanf("%d%d", &column, &row) == 2 && column > 0 && row > 0)
        addition(column, row);

    return 0;
}
Related