gpt4 book ai didi

c - Realloc:重置结构中的灵活结构数组

转载 作者:行者123 更新时间:2023-11-30 15:11:42 25 4
gpt4 key购买 nike

我一直用这个把头撞在墙上。我能够将其范围缩小到代码的重新分配部分。

CalStatus readCalComp( FILE *const ics, CalComp **const pcomp )
{
CalStatus test;
CalProp * foldLine;
CalProp * temp;
CalComp ** subComp;
char * buffer;
static int depth = 0;
int count = 0;
int i = 0;

buffer = NULL;
foldLine = NULL;
subComp = NULL;

if (depth == 0)
{
test = readCalLine (ics, &buffer);

if (buffer == NULL)
{
return test;
}
foldLine = malloc (sizeof (CalProp));
assert (foldLine != NULL);
test.code = parseCalProp (buffer, foldLine);
free (buffer);

if ((strcmp ("BEGIN", foldLine->name) == 0) && ((strcmp ("VCALENDAR", foldLine->value) == 0)))
{
(*pcomp)->name = malloc (sizeof(char) * strlen(foldLine->value)+1);
assert ((*pcomp)->name != NULL);
strcpy((*pcomp)->name, foldLine->value);
depth = depth + 1;

free(foldLine->name);
free(foldLine->value);
free(foldLine);

while ((buffer != NULL) && (test.code == OK) && (depth > 0))
{
test = readCalLine (ics, &buffer);

if (buffer != NULL)
{
foldLine = malloc (sizeof(CalProp));
assert (foldLine != NULL);
test.code = parseCalProp (buffer, foldLine);
free (buffer);

if ((strcmp ("END", foldLine->name) == 0) && ((strcmp ("VCALENDAR", foldLine->value) == 0) || (strcmp ("VCALENDAR\r\n", foldLine->value) == 0) ))
{
depth = depth - 1;

free(foldLine->name);
free(foldLine->value);
free(foldLine);

return test;
}
else if (strcmp ("BEGIN", foldLine->name) == 0)
{
subComp = malloc(sizeof (CalComp *));
*subComp = malloc(sizeof(CalComp) + (1*sizeof(CalComp*)));

(*subComp)-> name = NULL;
(*subComp)-> nprops = 0;
(*subComp)-> prop = NULL;
(*subComp)-> ncomps = 0;
(*subComp)-> comp[0] = NULL;

(*subComp)-> name = malloc(sizeof(char) *strlen(foldLine->value)+1);
strcpy((*subComp)->name, foldLine->value);

if ((*pcomp)-> ncomps == 0)
{
(*pcomp)->comp[(*pcomp)->ncomps] = *subComp;
(*pcomp)->ncomps += 1;
}
else
{
(*pcomp)->ncomps += 1;
*pcomp = realloc(*pcomp, sizeof(CalComp) + (2*sizeof(CalComp*)));
(*pcomp)->comp[(*pcomp)->ncomps-1] = *subComp;
}

depth = depth + 1;

test = readCalComp (ics, subComp);
}
} // end of if buffer NULL
}// end of While
} // end of if VCALENDAR
} // End of if Depth
else
{
if (count == 0)
{
test = readCalLine (ics, &buffer);
count += 1;
}

while ((test.code == OK) && (buffer != NULL))
{
if (count != 1)
{
test = readCalLine (ics, &buffer);
}
else
{
count = 0;
}

if (buffer != NULL)
{
foldLine = malloc (sizeof(CalProp));
test.code = parseCalProp (buffer, foldLine);
free (buffer);

if ((strcmp ("END", foldLine->name) == 0) && ((strcmp ((*pcomp)->name, foldLine->value) == 0)))
{
depth = depth - 1;

free (foldLine->name);
free (foldLine->value);
free (foldLine);

return test;
}
else if (strcmp ("BEGIN", foldLine->name) == 0)
{
printf("Success in malloc 2!\n");
subComp = malloc(sizeof (CalComp *));
*subComp = malloc(sizeof(CalComp) + (1*sizeof(CalComp*)));

(*subComp)-> name = NULL;
(*subComp)-> nprops = 0;
(*subComp)-> prop = NULL;
(*subComp)-> ncomps = 0;
(*subComp)-> comp[0] = NULL;

(*subComp)-> name = malloc(sizeof(char) *strlen(foldLine->value)+1);
strcpy ((*subComp)->name, foldLine->value);

if ((*pcomp)-> ncomps == 0)
{
(*pcomp)->comp[(*pcomp)->ncomps] = *subComp;
(*pcomp)->ncomps += 1;
}
else
{
(*pcomp)->ncomps += 1;
(*pcomp) = realloc(*pcomp, sizeof(CalComp) + (2*sizeof(CalComp*)));
(*pcomp)->comp[(*pcomp)->ncomps-1] = *subComp;
printf ("First subcomponent is %s\n", (*pcomp)->comp[0]->name);
printf ("Second subcomponent is %s\n", (*pcomp)->comp[1]->name);
}

depth = depth + 1;

test = readCalComp (ics, subComp);
}
}
}
}

return test;
}

CalComp 是一个结构体,包含:

char * name;

int numProps;

CalProp *prop (linked list);

int numComp;

CalComp *comp[] (flexible array);

检查函数内部,灵活数组中的结构名称是正确的,但是当我尝试访问函数外部的名称时,它要么为 NULL 值,要么被设置为第一个链表节点的名称结构。

由于赋值的性质,我无法修改 Calcomp 结构或函数参数。该函数必须是递归的,并且灵活的数组必须根据需要进行调整。

正如我之前所说,我通过打印灵活数组中的值将范围缩小到重新分配。该错误发生在程序重新分配时。

仅供引用,我尝试重新分配给临时变量,检查是否为 NULL,然后将该变量分配给 pcomp,但没有帮助。

最佳答案

当您调用 realloc 时,您不会将 ncomp 包含在灵活结构的附加大小中,您只是将其硬编码为 2 .

(*pcomp) = realloc(*pcomp, sizeof(CalComp) + (*pcomp->ncomps * sizeof(CalComp*)));

关于c - Realloc:重置结构中的灵活结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35514471/

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