gpt4 book ai didi

c - 为什么我会收到 "corrupted double-linked list"错误?

转载 作者:太空宇宙 更新时间:2023-11-04 03:27:11 26 4
gpt4 key购买 nike

程序

我正在用 C 语言编写一个程序,该程序读取多项式系数的文档并计算多项式的根。

在我的一个函数中,我试图通读文本文件,并创建一个“多项式”列表。多项式定义如下:

typedef struct
{
unsigned int nterms; /* number of terms */
double complex *polyCoef; /* coefficients */
} polynomial;

文本文件采用以下格式,其中每一行代表一个多项式,每个数字代表一个系数:

1     0     0     0     2    -1
16 70 -169 -580 75
1 0 4 0 -5
0 -9 3 5 -3
5 -4 3 -2 0
1.0 -3.4 5.4531 -4.2077 1.5092 -0.2030

问题

我在实现尝试中遇到了一些奇怪的行为。使用以下代码,我得到

* Error in `./hw6': corrupted double-linked list: 0x0000000000a1c240 *

    PolyFile = fopen(argv[2], "r"); 

if(NULL == PolyFile){ /* If the file fails to open */
fprintf(stderr, "Error: Input file '%s' not found\n", argv[2]);
return(-1);
}

/****** Read in the polynomials *************/
polynomials = malloc(sizeof(polynomial*) * size); /* Initialize */

if(polynomials == NULL){
fprintf(stderr, "%s %i: Could not allocate memory\n", __FILE__, __LINE__);
exit(-99);
}

/* Read all of the data from the file */
while(fgets(String, MAX_STR_LEN, PolyFile)) {
strLen = strlen(String); /* Determine size of line */

/* Ensure that the line is not too long */
if(strLen <= MAX_STR_LEN){
/* Create the polynomial */
p = (polynomial*)malloc(sizeof(polynomial));

token = strtok(String, " ");

while(token){
coefficients[coeffCount] = token;
token = strtok(NULL, " ");
coeffCount++;
}
createPoly(p, coeffCount);

/* Set p->polyCoef to the reverse of coefficients */
for(int lcv = 0; lcv <= coeffCount - 1; lcv++){

p->polyCoef[lcv] = atof(coefficients[coeffCount - lcv - 1]) + 0.00*I;
}
coeffCount = 0;

polynomials[size] = p;
printf("P->NTERMS: %i\n", p->nterms);
size++;

} else {
fprintf(stderr, "%s %i: Line too long. Polynomial ignored\n",
__FILE__, __LINE__);
}

}
fclose(PolyFile);
/**************************/
for(int j = 0; j <= size - 1; j++){
printf("J: %i\n", j);
printf("IN FOR LOOP: %i\n", (polynomials[j])->nterms);
// printPoly(polynomials[j]);
printf("\n");
}

. . .

/*---------------------------------------------------------------------------
Creates a polynomial data structure with nterms. This allocates storage
for the actual polynomial.

Where: polynomial *p - Pointer to polynomial data structure to create
unsigned int nterms - The number of elements to create
Returns: nothing
Errors: prints an error and exits
---------------------------------------------------------------------------*/
void createPoly(polynomial *p, unsigned int nterms){
int lcv; /* loop control variable */

/* Create a polynomial struct */
p->nterms = nterms;
p->polyCoef = (double complex*)malloc(sizeof(double complex)*nterms);

/* Error out if problem with malloc */
if(p->polyCoef == NULL){
fprintf(stderr, "%s %i:Error allocating memory\n", __FILE__, __LINE__);
exit(1);
}

/* Set the coefficients to 0 */
for(lcv = 0; lcv < nterms; lcv++){
(p->polyCoef)[lcv] = 0.00 + 0.00*I;
}
}

如果我删除 fclose(PolyFile); ,代码继续,但调试打印显示 polynomials[0]->nterms等于一个随机的、变化的、非常大的数。我不知道为什么会这样。

最佳答案

出现问题是因为在初始化多项式时,size等于0:

polynomials = malloc(sizeof(polynomial*) * size);

这是通过使用文件中的行数分配适当的大小来解决的。

关于c - 为什么我会收到 "corrupted double-linked list"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40228230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com