我已经在这个论坛上搜索了几个小时,试图找到我的问题的答案,阅读手册页,似乎看不到我正在做的事情有什么问题。
这就是发生的事情。我有一个程序,如果两个对象之间的距离足够小,它会循环遍历并将条目添加到(图)邻接矩阵。如果它们不够小,不足以添加到邻接矩阵中,如果它们不是太大,则应该将条目的坐标添加到坐标对列表中,称为"possible_edgelist"。
我的问题是"possible_edgelist"不接受我分配给它的值(I和j到它的条目0和1)。然而,分配的两边的两个命令正在执行。我将在代码后面粘贴一些示例输出:
uncertain_edge_max = 4;
certain_edge_max = 3.2;
printmatrix(shelldistmtx, shellsize, shellsize); //this is being generated correctly.
max_num_of_edges = shellsize*(shellsize-1)/2;
possible_edgelist=(int**)malloc(max_num_of_edges* sizeof(int*));
for(i=0; i<max_num_of_edges; i++)
{
possible_edgelist[i] = (int*)malloc(2 * sizeof(int));
}
poss_edge_num = 0;
for(i=0; i<shellsize; i++)
{
adjacency_matrix[i][i]=0;
for(j=i+1; j<shellsize; j++)
{
if (shelldistmtx[i][j]<=certain_edge_max) {
adjacency_matrix[i][j]= 1;
adjacency_matrix[j][i]= 1;
}
else
{
if(shelldistmtx[i][j] <= uncertain_edge_max)
{
printf("%d t %d t %d t %lf n", poss_edge_num, i, j, shelldistmtx[i][j]);
// ^this is being executed
//*****THESE TWO LINES ARE NOT BEING EXECUTED???*********
possible_edgelist[poss_edge_num][0]=i;
possible_edgelist[poss_edge_num][1]=j;
poss_edge_num=poss_edge_num + 1; //this is being executed
}
adjacency_matrix[i][j] = 0;
adjacency_matrix[j][i] = 0;
}
}
adjacency_matrix[i][shellsize] = 1;
adjacency_matrix[shellsize][i] = 1;
}
//troubleshooting output
for (i=0; i<poss_edge_num; i++)
{
printf("%d, %d n", possible_edgelist[poss_edge_num][0], possible_edgelist[poss_edge_num][1]); //this outputs zeros and bizarre numbers.
}
现在,对于输出,我得到:
0.000000 3.535534 3.535534 4.960403 4.960403
3.535534 0.000000 5.000000 3.715589 3.715589
3.535534 5.000000 0.000000 3.715589 3.715589
4.960403 3.715589 3.715589 0.000000 3.400000
4.960403 3.715589 3.715589 3.400000 0.000000
0 0 1 3.535534
1 0 2 3.535534
2 1 3 3.715589
3 1 4 3.715589
4 2 3 3.715589
5 2 4 3.715589
6 3 4 3.400000
0, -1073741824
0, -1073741824
0, -1073741824
0, -1073741824
0, -1073741824
0, -1073741824
0, -1073741824
最后一组值应该是(0,1)、(0,2)、(1,3)等。我不知道为什么值没有被正确分配,有人能解释一下这里发生了什么吗?
我能想到的唯一一件事是,这发生在一个快速连续多次调用的函数中,但我确信free()这里的所有内容在返回之前随每个函数调用而变化,(以及不改变的东西,在函数外部分配/释放)。
哦,是的,我也试过calloc()。同样的问题,除了全是0。
帮助!
您的打印forl循环出现错误:
for (i=0; i<poss_edge_num; i++)
{
printf("%d, %d n", possible_edgelist[i][0], possible_edgelist[i][1]);
}