简体   繁体   English

尝试调整2D阵列大小时出现分段错误

[英]Segmentation fault when trying to resize a 2D Array

This is the code: 这是代码:

int **mat(int nl, int nc) {
  int i;
  int **v = malloc(nl * sizeof(int *));

  for (i = 0; i < nl; i++) {
    v[i] = calloc(nc, sizeof(int));
  }
  return v;
}


void crop(int **v,int *nl,int *nc,int l1,int c1,int l2,int c2)

{

int i,j;

for(i=0;i<(l2-l1);i++)
    for(j=0;j<(c2-c1)*3;j++)
        v[i][j]=v[l1+i][c1*3+j];

for(i=l2-l1;i<*nl;i++)
    free(v[i]);

v=realloc(v,(l2-l1)*sizeof(int *));

for(i=0;i<*nl;i++)
    v[i]=realloc(v[i],(c2-c1)*3*sizeof(int));

int x=l2-l1,y=c2-c1;
*nl=x;
*nc=y;

}


void resize(int **v,int *nl,int *nc,int nw,int nh)
{
int i,j,h=*nl,w=*nc,x=nh,y=nw;

if(nh>h && nw<=w)
    {
        crop(v,&w,&h,0,0,*nl,nw);
        v=realloc(v,nh*sizeof(int *));
        for(i=*nl;i<nh;i++)
        {
        v[i]=calloc(nw*3,sizeof(int));
        for(j=0;j<=nw*3;j++)
        v[i][j]=255;
        }

    }
    if(nh<=h && nw>w)
    {
        crop(v,&w,&h,0,0,nh,*nc);
        for(i=0;i<nh;i++)
        {
        v[i]=realloc(v[i],nw*3*sizeof(int));
        for(j=*(nc)*3;j<=nw*3;j++)
        v[i][j]=255;
        }
    }
    *nl=x;
    *nc=y;
}

int main(){

int nl,nc,i,j;
scanf("%d%d",&nc,&nl);
int **p = mat(nl,nc*3);

for(i=0;i<nl;i++)
    for(j=0;j<nc*3;j++)
        p[i][j]=i+j;

resize(p,&nl,&nc,2,4);

for(i=0;i<nl;i++)
{
    for(j=0;j<nc*3;j++)
    printf("%d ",p[i][j]);
    printf("\n");
}

Let nc=2,nl=3 nc=2,nl=3

So, when I call in main resize(p,&nl,&nc,4,2) , the resize function goes in the second if it first crops the bottom of the matrix cause the new height is smaller that the old height ( nh > nl ), after the crop it goes on the remaining lines (2) and it realloc s memory so that it can fill the new width with {255,255,255} and it all goes well. 因此,当我调用main resize(p,&nl,&nc,4,2) ,如果它首先裁剪矩阵的底部,导致新高度小于旧高度( nh > nl ),在裁剪后继续在其余的行(2)上,并重新realloc内存,以便可以用{255,255,255}填充新宽度,并且一切顺利。

However, when I call resize(p,&nl,&nc,2,4) , it goes on the first if, the debugger show no errors and it is filling up the 2D Array, but when it gets to printf in main, I get segmentation fault. 但是,当我调用resize(p,&nl,&nc,2,4) ,它会在第一个条件上继续运行,如果调试器未显示任何错误,并且它正在填充2D数组,但是当它进入printf中的printf时,我得到了分段故障。 What could be the problem? 可能是什么问题呢?

As BLUEPIXY hinted at with void crop --> int **crop and return v; 正如BLUEPIXY所提示的那样,使用void crop -> int **cropreturn v; , we must take into account that realloc may move the area pointed to, thus a function which calls realloc for your row pointer array has to return the possibly changed memory pointer, and the calling function is to reassign it with the returned value. ,我们必须考虑到realloc可能会移动指向的区域,因此,为您的行指针数组调用realloc的函数必须返回可能已更改的内存指针,并且调用函数将使用返回的值对其进行重新分配。 This applies not only to the function crop() , but also to resize() , so the necessary changes are: 这不仅适用于函数crop() ,还适用于resize() ,因此必要的更改是:

int **crop(int **v, int *nl, int *nc, int l1, int c1, int l2, int c2)
{
    …
    return v;
}

int **resize(int **v, int *nl, int *nc, int nw, int nh)
{
    …
        v = crop(v, &w, &h, 0, 0, *nl, nw);
    …
        v = crop(v, &w, &h, 0, 0, nh, *nc);
    …
    return v;
}

int main()
{
    …
    p = resize(p, &nl, &nc, 2, 4);
    …
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试连接二维数组的元素时出现分段错误 - Segmentation fault when trying to concatenate an element of a 2d array 二维数组中的分段错误 - segmentation fault in 2d array 尝试将数组从stdin复制到2d数组时,为什么会出现分段错误? - Why do I get a segmentation fault when trying to copy an array from stdin to a 2d array? 尝试通过指针数组从二维数组打印字符串时出现分段错误 - Segmentation fault when trying to print strings from 2D array via array of pointers 尝试从文件读入二维数组时出现分段错误 - segmentation fault while trying to read in from file to a 2D array 尝试传递递归函数以填充2D数组时出现分段错误 - Segmentation fault on trying to pass a recursive function to populate a 2D array 尝试在C中访问2D数组的地址并出现分段错误 - Trying to access address of 2D array in C and getting segmentation fault 尝试将stdin读取到2d动态分配的数组时出现分段错误 - Segmentation fault when trying to read stdin to a 2d dynamically allocated array 尝试对2D char数组使用strcpy()时遇到分段错误? - Getting a segmentation fault when trying to use strcpy() to a 2D char array? 尝试从2D数组中的字符串获取字符时出现分段错误 - Segmentation fault when trying to get a character from a string in a 2D array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM