A little addition: the segmentation fault seems to come from another recursion that appears below the above statement. I'm updating the question with the recursive function, since that is small.
What is the reason of Segmentation Fault in matlab?
3 次查看(过去 30 天)
显示 更早的评论
I'm running a code which was working fine until I replaced some part of it with a recursive function. After introducing it, the same code with minimal change started giving segmentation fault error on every run. I tried debugging by instrumenting the code with breakpoints and some print statements, and I find that the trouble is not so much with the recursive function. The code is large, that is why I thought of not putting it here. But the rough structure is as follows:
while searchtime < MaxTime
state = computation of some parameters;
value = recursive(state);
compute some more stuff using the value;
searchtime = searchtime + 1;
end
I have used the diary option to save the terminal log, and I see that when it moves from the first searchtime to the next in the while loop, the segmentation fault occurs. Is it because the memory used by the recursive function does not get cleared automatically within two runs of the while loop? What is the workaround in such a case? I'm running Matlab 7.10.0 (R2010a). Thanks in advance
The segmentation fault comes from the code below:
for nodes = 1:n
if SearchEndTime(locationx(nodes),locationy(nodes))>0
Effort(nodes) = SearchEndTime(locationx(nodes),locationy(nodes)) - recruitedTime(nodes);
RegionEffort(locationx(nodes),locationy(nodes)) = RegionEffort(locationx(nodes),locationy(nodes)) + Effort(nodes);
if sum(RecruitmentTree(nodes,:)) == 0
isLeaf(nodes) = 1;
end
end
end
for nodes = 1:n
if RegionEffort(locationx(nodes),locationy(nodes))
rewardOfEachNode(nodes,1) = Effort(nodes)/RegionEffort(locationx(nodes),locationy(nodes));
rewardOfEachNode(nodes,2) = rewardOfEachNode(nodes,1) * PerFlagReward/2;
rewardOfEachNode(nodes,3) = rewardOfEachNode(nodes,1) * PerFlagReward/2;
end
end
set(0,'RecursionLimit',2000)
for rootnodes = k
recursiveRewardComputeDFS(rootnodes) ;
end
The function is as follows:
function [value] = recursiveRewardComputeDFS(currentnode)
global isLeaf;
global rewardOfEachNode;
global x;
global y;
global RecruitmentTree;
if isLeaf(currentnode) == 0
childrenofcurrentnode = find(RecruitmentTree(currentnode, :) == 1);
for child = childrenofcurrentnode
recursiveRewardComputeDFS(child);
rewardOfEachNode(currentnode,2) = rewardOfEachNode(currentnode,2) + rewardOfEachNode(child,2)/2;
rewardOfEachNode(currentnode,3) = rewardOfEachNode(currentnode,3) + exp(-dist(currentnode, child, x, y)) * rewardOfEachNode(child,3)/2;
end
end
end
2 个评论
Walter Roberson
2011-9-2
Increasing the recursion limit can cause unpredictable behavior if the memory for the recursion exceeds the stack size.
回答(2 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Software Development Tools 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!