我正在尝试动态更改图表矩阵的大小。
代码:
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);
有谁知道我做错了什么?
答案 0 :(得分:0)
您是否检查过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?。如果我误读你的意图,请纠正我。