gpt4 book ai didi

c++ - 递归函数返回列表中的段错误

转载 作者:太空狗 更新时间:2023-10-29 23:17:51 25 4
gpt4 key购买 nike

我有一个递归函数,它返回一个结构列表。

struct Neighbour_node{
int index;
double dist;
};

函数如下:

list<Neighbour_node> findNewNeighbours(int original, int particle, int k){
Neighbour_node node;
list<Neighbour_node> neighbours;
list<Neighbour_node> temp_neighbours;
list<Neighbour_node>::iterator iterator;

if (k <= 0){
if (particle == -1){
node.index = -1;
node.dist = 1000.0;
}
else{
node.index = particle;
node.dist = glm::length(hair[original].position - hair[particle].position);
neighbours.push_back(node);
}
}
else {
for (unsigned int i = 0; i < hair[particle].neighbours.size(); i++){
temp_neighbours = findNewNeighbours(original,hair[particle].neighbours[i],k - 1);

temp_neighbours.sort(compareNeighbour_node);
neighbours.merge(temp_neighbours,compareNeighbour_node);
}
}
return neighbours;
}

线路:

temp_neighbours = findNewNeighbours(original,hair[particle].neighbours[i],k - 1);

导致段错误,我不确定为什么。我见过与我错误的例子相似的例子,看起来并没有错。但是这些函数不是递归的,所以我猜这就是问题所在——此外,当 k = 0 时(只有一个函数调用——因此就好像它不是递归的),那么它不会崩溃。谁能帮我解决这个问题?谢谢

最佳答案

检查操作系统中的堆栈大小。

ulimit -s

我建议是因为stack。因为您需要的堆栈似乎在迅速增加。

展示你的“头发”,让我们看到更多细节。

关于c++ - 递归函数返回列表中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16310480/

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