cmatrixsegmentation-faultmemory-reallocation

Segmentation fault (core dumped) error in dynamic memory allocation


I'm new to coding and new to C++. I am trying to write code in which one enters elements of the matrix. Afterwards, one column should be removed, and one row should be added. Removing one column works fine. However, after reallocating memory for the new number of rows, I get the message: segmentation fault (core dumped). I am using pointers to create and edit my matrix. Here is my code. Thanks for any help!

#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,r,**p,column,row;
printf("Enter the number of rows and columns:\n");
scanf("%d\n%d",&r,&c);
p=(int**)calloc(r,sizeof(int*));
if(p==NULL) printf("Memory not allocated.\n");
else{
    for(int i=0;i<r;i++){
        *(p+i)=(int*)calloc(c,sizeof(int));
        printf("Enter %d. row\n",i+1);
        for(int j=0;j<c;j++){
            scanf("%d",*(p+i)+j);
        }
    }
    printf("Original matrix:\n");
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            printf("%d ",*(*(p+i)+j));
        }
        printf("\n");
    }

    printf("Which column do you want to remove?");
    scanf("%d",&column);
    while(column<1||column>c){
        printf("Wrong entry, enter again:");
        scanf("%d",&column);
    }
    for(int i=0;i<=r-1;i++){
        for(int j=column-1;j<=c-2;j++)
            *(*(p+i)+j)=*(*(p+i)+j+1);
        *(p+i)=(int*)realloc(*(p+i),(c-1)*sizeof(int));
    }
    printf("Matrix without %d. column:\n",column);
    for(int i=0;i<r;i++){
        for(int j=0;j<c-1;j++)
            printf("%d ",*(*(p+i)+j));
        printf("\n");
    }
    printf("Which row do you want to replace?\n");
    scanf("%d",&row);
    while(row<1||row>r){
        printf("Wrong entry, enter again:\n");
        scanf("%d",&row);
    }
    p=(int**)realloc(p,(r+1)*sizeof(int*));
    if(p==NULL)
        printf("Memory not allocated.\n");
    else{
        printf("Enter %d. row",row);
        for(int i=r+1;i>row-1;i++){
            *(p+i)=*(p+i-1);
        }
        for(int j=0;j<c-2;j++)
            scanf("%d",*(p+row-1)+j);
        printf("New matrix:\n");
        for(int i=0;i<=r;i++){
            for(int j=0;j<c-2;j++)
                printf("%d ",*(*(p+i)+j));
            printf("\n");
        }
    }
    }
    return 0;
    }

Solution

  • When you realloc memory for p, you add one new row to that array (one additional pointer). But you never allocate the memory for that new row, and eventually you access this uninitialized pointer.

    But that's not the real problem. In the loop where you want to bump the pointers up to make space for the inserted row, your initial value and loop increment are both wrong. With an initial value of int i=r+1, the first write to *(p+i) will access one past the allocated space (which would be p[0] thru p[r]). The loop increment should be a decrement, --i. Once this loop is done, then you can allocate space for the new row.