[英]segmentation fault double pointer realloc
I've got a small part of simple code that keeps giving me segmentation fault each time I go through the realloc for loop in the second if statement.我有一小部分简单的代码,每次我在第二个 if 语句中通过 realloc for 循环时,都会不断给我分段错误。
*x = (int**)realloc(*x,nNew*sizeof(int*));
for(int i=0;i<n;i++)
x[i] = (int*) realloc(x[i],nNew*sizeof(int)); // seg fault
I think that the memory allocation has been done correctly (apparently not) but I cannot find the error.In the first if statement (where I de-allocate memory) it works correctly.我认为内存分配已正确完成(显然不是),但我找不到错误。在第一个 if 语句(我取消分配内存的地方)它工作正常。
Sorry for the bad format.抱歉格式不正确。
void update(int **x,int n,int nNew)
{
int temp;
if(n>nNew)
{
*x = (int **) realloc(*x,nNew*sizeof(int*));
for(int i=0;i<n;i++)
*(x+i) =(int *) realloc(x[i],nNew*sizeof(int));
printf("temp %d",x[nNew-1][nNew-1]);
}
else if(n<nNew)
{
temp = x[n-1][n-1];
*x = (int**)realloc(*x,nNew*sizeof(int*));
for(int i=0;i<n;i++)
x[i] = (int*) realloc(x[i],nNew*sizeof(int)); // seg fault
printf("temp %d",**temp);
for(int i=n;i<(nNew);i++)
{
for(int j=n;j<(nNew);j++)
x[i][j] = temp;
}
}
}
int main()
{
int nNew = 9;
int n = 8;
int **x = (int **) malloc(n*sizeof(int*));
for (int i=0; i<n; i++)
x[i] = (int *)malloc(n * sizeof(int));
int count=0 ;
for(int i=0;i<n;i++)
{
count ++;
for(int j=0;j<n;j++)
x[i][j] = count;
}
update(x,n,nNew);
for(int i=0;i<nNew;i++)
free(x[i]);
free(x);
return 0;
}
You're not reallocating what you think you are.你并没有重新分配你认为你是什么。
When you do this:当你这样做时:
*x = (int**)realloc(*x,nNew*sizeof(int*));
for(int i=0;i for(int i=0;i
Since x
has type int **
, *x
has type int *
.由于
x
类型为int **
, *x
类型为int *
。 Also, *x
is the same as x[0]
.此外,
*x
与x[0]
相同。 So you're not reallocating the array of pointers on the first line but the first array of int
.因此,您不是在第一行重新分配指针数组,而是重新分配
int
的第一个数组。 You need to call realloc
on the original pointer, and you need to pass the address of that pointer so that the change is visible in the calling program.您需要对原始指针调用
realloc
,并且需要传递该指针的地址,以便在调用程序中可以看到更改。
Then for the reallocation of the individual arrays, you need to call realloc
on the original n
to grow or shrink them, then either free
the extra rows if growing or malloc
the newly added rows.然后对于单个数组的重新分配,您需要对原始
n
调用realloc
以增大或缩小它们,然后在增长时free
额外的行或malloc
新添加的行。 In the growing case you want to realloc
the array of pointers fisrt, and in the shrinking case you want to do that last.在不断增长的情况下,你要
realloc
最前一页指针数组,并在萎缩的情况下,你想这样做最后一次。
So after those changes your update
function should look like this:因此,在这些更改之后,您的
update
函数应如下所示:
void update(int ***x, int n, int nNew)
{
if(n>nNew) {
// shrink each row to be kept
for(int i=0;i<nNew;i++)
(*x)[i] = realloc((*x)[i],nNew*sizeof(int));
// free the extra rows
for(int i=nNew;i<n;i++)
free((*x)[i]);
// shrink the list of rows
*x = realloc(*x,nNew*sizeof(int*));
} else if(n<nNew) {
// grow the list of rows
*x = realloc(*x,nNew*sizeof(int*));
// grow the existing rows
for(int i=0;i<n;i++)
(*x)[i] = realloc((*x)[i],nNew*sizeof(int));
// create the new rows
for(int i=n;i<nNew;i++)
(*x)[i] = malloc(nNew*sizeof(int));
}
}
And you call it like this:你这样称呼它:
update(&x,n,nNew);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.