我现在已经调试了这个问题很长时间了,也在网上搜索过,没有找到任何帮助。如果有人能帮助我找出这个错误,我将非常感激:
我正在为图形构建一个数据库,其中 while循环读取命令的stdin 。命令可以是顶点之间的Add_Vertex和Add_Edge。 我注意到在while循环的迭代之间,内存以某种方式被改变。
所以我做了一个调试打印函数,它在迭代结束时和下一个函数的开头打印完全相同的东西。瞧,印刷品不同。这些是印刷品:
____________________________________________________________________
started new iteration. current status:
still no keys in the graph
_________ Executing: Add_Vertex v5 -2 -5
finished executing. current status before next iteration of while loop:
key1 = v5
first connection of v5 is (null)
____________________________________________________________________
started new iteration. current status:
key1 = v5
first connection of v5 is (null)
_________ Executing: Add_Vertex v4 4 4
finished executing. current status before next iteration of while loop:
key1 = v4
first connection of v4 is (null)
key2 = v5
first connection of v5 is (null)
____________________________________________________________________
started new iteration. current status:
key1 = v4
first connection of v4 is (null)
key2 = v5
first connection of v5 is (null)
_________ Executing: Add_Edge v4 v5
finished executing. current status before next iteration of while loop:
key1 = v4
first connection of v4 is v5
key2 = v5
first connection of v5 is v4
____________________________________________________________________
started new iteration. current status:
key1 = v4
first connection of v4 is 3 0 5
key2 = v5
first connection of v5 is x v3 0 5
_________ Executing: Add_Vertex v3 0 5
finished executing. current status before next iteration of while loop:
key1 = v3
first connection of v3 is (null)
key2 = v4
first connection of v4 is 3
key3 = v5
first connection of v5 is x
注意最后一次迭代的打印方式与之前的不同。
这是源代码:
#define MAX_LINE_SIZE 256
int main()
{
char szLine[MAX_LINE_SIZE];
char* delimiters = " \t\n";
char* pszCommand;
// ...
PGRAPH pg;
pg = GraphCreate();
while (fgets(szLine, MAX_LINE_SIZE, stdin))
{
printf("________________________________________________________\n started new iteration. current status:\n");
debugprints(pg);
printf("_________ Executing: %s", szLine);
pszCommand = strtok(szLine, delimiters);
if (NULL == pszCommand )
{
continue;
}
if ( 0 == strcmp(pszCommand, "Add_Vertex") )
{
// Add Vertex To graph ...
}
else if ( 0 == strcmp(pszCommand, "Add_Edge") )
{
// Add The Desired Edge ...
}
printf("finished executing. current status before next iteration of while loop:\n");
debugprints(pg);
}
GraphDestroy(pg);
return 0;
}
这是debugprints的代码(它只打印图表的信息):
void debugprints(PGRAPH gr) {
char* key1 = (char*)ListGetFirstKey(gr->pl);
char* key2 = (char*)ListGetNextKey(gr->pl,key1);
char* key3 = (char*)ListGetNextKey(gr->pl,key2);
PLIST adj_list1 = NULL;
PLIST adj_list2 = NULL;
PLIST adj_list3 = NULL;
if (key1) {
printf("\tkey1 = %s\n",key1);
adj_list1 = (PLIST)(((PADJ_LIST)ListGetData(gr->pl,key1))->pEdges);
if (adj_list1) printf("\tfirst connection of %s is %s\n",key1,(char*)ListGetFirstKey(adj_list1));
if (key2) {
printf("\tkey2 = %s\n",key2);
adj_list2 = (PLIST)(((PADJ_LIST)ListGetData(gr->pl,key2))->pEdges);
if (adj_list2) printf("\tfirst connection of %s is %s\n",key2,(char*)ListGetFirstKey(adj_list2));
if (key3) {
printf("\tkey3 = %s\n",key3);
adj_list3 = (PLIST)(((PADJ_LIST)ListGetData(gr->pl,key3))->pEdges);
if (adj_list3) printf("\tfirst connection of %s is %s\n",key3,(char*)ListGetFirstKey(adj_list3));
}
}
} else printf("\tstill no keys in the graph\n");
}
谢谢!!!