gpt4 book ai didi

null - NULL 检查中的段错误?

转载 作者:行者123 更新时间:2023-12-03 08:23:33 29 4
gpt4 key购买 nike

我在尝试检查某些元素是否为 NULL 时遇到段错误或不。任何人都可以帮忙吗?

    void addEdge(int i, int j) 
{
if (i >= 0 && j > 0)
{
Node* whereto;
whereto = linkedAdjacencyList[i];
if(whereto != NULL)
{
while(whereto->adj != NULL)
{whereto = whereto->adj;}
whereto->adj = linkedAdjacencyList[j];
}
else{linkedAdjacencyList[i]->adj = linkedAdjacencyList[j];}
whereto = linkedAdjacencyList[j];
if(whereto != NULL)
{
while(whereto->adj != NULL)
{whereto = whereto->adj;}
whereto->adj = linkedAdjacencyList[i];
}
else{linkedAdjacencyList[j]->adj = linkedAdjacencyList[i];}
}
}

帮助!

编辑:根据您的建议,这是新代码,但是现在调用该方法时立即出现段错误?我会发布电话...
int edges;
in >> edges;
g.edges = edges;
for(int i = 0; i < edges; i++)
{
int first;
int second;
in >> first >> second;
g.addEdge(first, second);
}

最佳答案

我建议 linkedAdjacencyList[i]返回 NULL ,所以你试图取消引用 NULL .只需为 NULL 添加额外检查:

Node* whereto = NULL;
whereto = linkedAdjacencyList[i];
if (NULL != whereto){
while(whereto->adj != NULL) .....

关于null - NULL 检查中的段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5643854/

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