gpt4 book ai didi

c - C 中的动态矩阵重新分配 - 在访问冲突读取位置 (msvcr120d.dll) 出现错误未处理的异常

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

我正在尝试动态更改图形矩阵的大小。

代码:

void addNumToGraph(vertex** tracker, int inNum, int i, int** graphMatrix)
{
tracker = (vertex**)realloc(tracker, i*sizeof(vertex*));
if (sizeof(tracker)/sizeof(vertex*) < inNum)
{
graphMatrix = (int**)realloc(graphMatrix, sizeof(int*)*inNum);
for (i = 0; i < sizeof(graphMatrix)/sizeof(int*); i++)
{
//I am getting the error on the line below
graphMatrix[i] = (int*)realloc(graphMatrix[i], sizeof(int)*inNum);
}
}
tracker[i]->color = 0;
tracker[i]->distance = -1;
tracker[i]->father = NULL;
tracker[i]->value = inNum;
tracker[i]->index = i;
}

上面的函数是从 while 循环中调用的:

while (inChar != '|')
{
//will scan the vertex number
scanf("%d", &inNum);
addNumToGraph(tracker, inNum, i, graph);
i++;
//will scan the ','
scanf("%c", &inChar);
}

它位于另一个函数中。

graph 是函数中的局部双指针- 我尝试了两种方法:将图形初始化为 NULL 并使用 malloc 对其进行初始化,这样它就不会为空

两者的评估结果相同:

错误 -

"getting error Unhanded exception at (msvcr120d.dll) in Access violation reading location......"

错误每次都出现在同一行代码:

graphMatrix[i] = (int*)realloc(graphMatrix[i], sizeof(int)*inNum);

有人知道我做错了什么吗?

最佳答案

您是否检查过 sizeof() 是否返回正确的值?当数组是函数的参数时,sizeof 没有正确报告数组的大小:http://c-faq.com/aryptr/aryparmsize.html .您也可以在这里查看:When a function has a specific-size array parameter, why is it replaced with a pointer? .如果我误解了您的意图,请纠正我。

关于c - C 中的动态矩阵重新分配 - 在访问冲突读取位置 (msvcr120d.dll) 出现错误未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502825/

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