Thread: Adding rows in an array created with malloc()

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    3

    Adding rows in an array created with malloc()

    Hello everybody,
    I want to write a function which takes an array previously created with allocated memory with a given number of rows and columns. This function should increase the number of rows and columns, fill the array and update it in the original array, also the number of rows and columns should be updated. I created the function update() but I have two problems:
    1. When I print the updated array inside the function update(), everything looks ok. But when I print the array in the main() the result is not he expected one. It is curious for me that the number of rows and columns are being properly updated but the array is not updated.
    2. I am creating the array MATRIX with malloc() in the main(). Then the function update() is taking MATRIX is reallocating and doing everything well, but I still need to liberate the memory created with malloc(), if I put the instruction to liberate the memory in the main, I get an error saying that I am trying to free memory which was not allocated.

    Follows the code that I wrote:
    Code:
    int main() {
        int rows=1, cols=1;
        int * MATRIX = (int*)malloc(rows * cols * sizeof(int));
        *MATRIX=1;
        update(&rows, &cols, MATRIX);
        printf("\n\n (IN THE MAIN) rows=%d\n\t\t\tcolumns=%d\n",rows, cols);
        printf("\n Printing the array in the main()\n");
        for (int i=0;i<rows;i++){
            for (int j=0;j<cols;j++){
                printf("%3d",*(MATRIX + i*cols + j));
            }
            printf("\n");
        }
        //free(MATRIX);
        return 0;
    }
    
    
    void update(int * nrow, int * ncol, int * Matrix){
        int Nrow=*nrow*3;
        int Ncol=*ncol+1;
        int * NewMatrix=(int*)malloc(Nrow * Ncol * sizeof(int));
        for (int i=0;i<Nrow;i++){
            for (int j=0;j<Ncol;j++){
                *(NewMatrix+ i*Ncol +j)=i*Ncol + j;
            }
        }
        Matrix=(int *)realloc(Matrix,(Nrow * Ncol * sizeof(int)));
        for (int i=0;i<Nrow;i++){
            for (int j=0;j<Ncol;j++){
                *(Matrix+ i*Ncol +j)=*(NewMatrix+ i*Ncol +j);
            }
        }
    free(NewMatrix);
        printf("Printing the result INSIDE the function\n");
        for (int i=0;i<Nrow;i++){
            for (int j=0;j<Ncol;j++){
                printf("%3d\t",*(Matrix+ i*Ncol +j));
            }
            printf("\n");
        }
        *nrow=Nrow;
        *ncol=Ncol;
        printf("\nrows=%d\ncols=%d",*nrow,*ncol);
    }
    The output obtained is:
    Printing the result INSIDE the function
    0 1
    2 3
    4 5

    rows=3
    cols=2

    (IN THE MAIN) rows=3
    columns=2

    Printing the array in the main()
    0 0
    268767505805306368
    5313504 1

    And if I enable the line free(MATRIX) in the main() then I get the following error:
    UPDATING_ARRAYS(8726,0x1000a95c0) malloc: *** error for object 0x10210c720: pointer being freed was not allocated.

    Thanks beforehand for your help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The matrix that update receives is only a copy of the pointer.
    So changing it locally doesn't change it in main.

    That also leads to the freeing problem, because the realloc will free the old pointer just fine, but that leads to a memory leak in update, and a stale pointer in main.

    Try
    Code:
    int main() {
        int rows=1, cols=1;
        int * MATRIX = malloc(rows * cols * sizeof(int));
        *MATRIX=1;
        update(&rows, &cols, &MATRIX);
        printf("\n\n (IN THE MAIN) rows=%d\n\t\t\tcolumns=%d\n",rows, cols);
        printf("\n Printing the array in the main()\n");
        for (int i=0;i<rows;i++){
            for (int j=0;j<cols;j++){
                printf("%3d",*(MATRIX + i*cols + j));
            }
            printf("\n");
        }
        free(MATRIX);
        return 0;
    }
    
    
    void update(int * nrow, int * ncol, int **Matrix){
        int Nrow=*nrow*3;
        int Ncol=*ncol+1;
        int * NewMatrix=malloc(Nrow * Ncol * sizeof(int));
        for (int i=0;i<Nrow;i++){
            for (int j=0;j<Ncol;j++){
                *(NewMatrix+ i*Ncol +j)=i*Ncol + j;
            }
        }
        (*Matrix)=realloc((*Matrix),(Nrow * Ncol * sizeof(int)));
        for (int i=0;i<Nrow;i++){
            for (int j=0;j<Ncol;j++){
                *((*Matrix)+ i*Ncol +j)=*(NewMatrix+ i*Ncol +j);
            }
        }
    
        free(NewMatrix);
        printf("Printing the result INSIDE the function\n");
        for (int i=0;i<Nrow;i++){
            for (int j=0;j<Ncol;j++){
                printf("%3d\t",*((*Matrix)+ i*Ncol +j));
            }
            printf("\n");
        }
        *nrow=Nrow;
        *ncol=Ncol;
        printf("\nrows=%d\ncols=%d",*nrow,*ncol);
    }
    Oh, and you don't need the casts on malloc/realloc - see the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    3
    Thank you very much!! That works!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-17-2015, 05:50 PM
  2. Replies: 1
    Last Post: 04-17-2015, 04:29 PM
  3. adding up the rows of a 2D array
    By youngvito in forum C Programming
    Replies: 31
    Last Post: 06-11-2009, 01:06 PM
  4. passing an array created with malloc as a param
    By mariano_donati in forum C Programming
    Replies: 12
    Last Post: 02-20-2008, 12:26 PM
  5. adding rows
    By RedZippo in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2004, 02:36 PM

Tags for this Thread