【发布时间】:2018-06-13 04:24:25
【问题描述】:
新年快乐!
一段时间以来,我一直在努力寻找导致错误的原因,我将在下面解释一段时间,我将非常感谢任何帮助。我有以下代码,原则上应该实现一个堆栈:
#include <stdio.h>
#include <stdlib.h>
void resize(int *nmax, int *d){
int i, *u;
*nmax *= 2;
u = (int *)realloc(d,sizeof(int)*(*nmax));
if(u == NULL){
printf("Error!\n");
exit(1);
}
d = u;
}
void push(int *n, int *d, int *nmax){
int u = *n , i;
if(u == *nmax) {
resize(nmax, d);
}
*(d + u) = u;
u++;
*n = u;
}
void pop(int *n){
int u = *n;
*n = u - 1;
}
int main(){
int *d, n = 0, i, nmax = 5;
d = (int *)malloc(sizeof(int)*nmax);
*(d+(n))= n;
n++;
*(d+(n)) = n;
n++;
*(d+(n)) = n;
n++;
//for(i = 0;i < n;i++)
//printf("%d\n",*(d+i));
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
push(&n,d, &nmax);
//pop(&n);
//pop(&n);
//pop(&n);
for(i = 0;i<n;i++)
printf("%d\n",*(d+i));
return 0;
}
它似乎表现正常,直到我在推送操作堆栈之前取消注释 printf 语句。我得到的错误是:
0
1
2
*** Error in `./a.out': realloc(): invalid next size: 0x0000000000f08010 ***
我不确定我是否已经解释过了,如果我没有解释清楚,请让我知道我可以添加的任何其他详细信息,以便更清楚地说明。
非常感谢您的帮助!
编辑:
希望代码现在变得更具可读性,这就是我所拥有的:
#include <stdio.h>
#include <stdlib.h>
void resize(int *sizeOfArray, int *myArray){
int i, *u;
*sizeOfArray *= 2;
u = (int *)realloc(myArray,sizeof(int)*(*sizeOfArray));
if(u == NULL){
printf("Error!\n");
exit(1);
}
myArray = u;
}
void push(int *pos, int *myArray, int *sizeOfArray){
int i;
if(*pos == *sizeOfArray) {
resize(sizeOfArray, myArray);
}
*(myArray + (*pos)) = *pos;
(*pos)++;
}
void pop(int *pos){
(*pos)--;
}
int main(){
int *myArray, pos = 0, i, sizeOfArray = 5;
myArray = (int *)malloc(sizeof(int)*sizeOfArray);
*(myArray + pos)= pos;
pos++;
*(myArray + pos) = pos;
pos++;
*(myArray + pos) = pos;
pos++;
//for(i = 0;i < pos;i++)
//printf("%d\n",*(myArray+i));
push(&pos, myArray, &sizeOfArray);
push(&pos, myArray, &sizeOfArray);
push(&pos, myArray, &sizeOfArray);
push(&pos, myArray, &sizeOfArray);
//pop(&pos);
//pop(&pos);
//pop(&pos);
for(i = 0;i<pos;i++)
printf("** %d\n",*(myArray+i));
return 0;
}
现在,错误发生了变化——这可能表明该方法存在问题——它显示:
我应该得到:
** 0
** 1
** 2
** 3
** 4
** 5
** 6
相反,我得到:
** 0
** 0
** 2
** 3
** 4
** 5
** 6
为什么?只有当我取消注释中间的 printf 时,我才会得到这个。
感谢您的帮助。
【问题讨论】:
-
d = u;仅更改传递给resize的本地值,然后被遗忘。 -
一堆什么?由于无意义的标识符,此代码非常不清楚。
d、n、u、nmax到底代表什么?您还使事情变得比必要的复杂得多(并且难以理解);例如所有那些显式的数组索引计算。为什么? -
@WeatherVane 非常感谢您的帮助!但是,如果我在推送操作之前没有“printf-ing”,我会得到一个“正常”的行为(至少,我看到了我预期我应该看到的)。如果你的建议是怎么回事,我大部分时间不应该得到错误的结果吗?
-
@meaning-matters 非常感谢您的评论。一堆什么都没有。这是我自己做的一个练习。我改变了所有的 d、n、u、nmax,所以它们以某种方式反映了我想要它们成为的样子。我希望代码提高了可读性。无论如何,我同意你的观点。
-
@Jean-FrançoisFabre 谢谢!