简体   繁体   English

ENTERING循环时出现分段错误

[英]segmentation fault when ENTERING loop

I have a seg fault in C++ when entering a for loop. 进入for循环时,C ++中出现段错误。 But I mean when ENTERING IT. 但是我的意思是输入时。 Here is the code I'm running: 这是我正在运行的代码:

std::cout<<"forcing order"<<endl;
std::cout<<"crossoverPointNumber = "<<crossoverPointNumber<<endl;
for (long j=0; j<crossoverPointNumber; j++)
{
    std::cout<<"j = "<<j<<". ";
    offsprings[1][positionsInParent1[j]] = valuesInParent2[j];  // Forces the order
}//end for j

The output I get on the terminal is: 我在终端上得到的输出是:

forcing order
crossoverPointNumber = 4
Segmentation fault

Can anyone explain to me what am I missing here?? 有人可以向我解释我在这里想念什么吗? it seems to be either very elementary or very complex C++ stuff... 它似乎是非常基础的或非常复杂的C ++东西...

You aren't adding an endl to the cout stream in your loop, so the code you've posted doesn't tell us when you are getting the segmentation fault. 您不会在循环中的cout流中添加endl ,因此您发布的代码不会在遇到分段错误时告诉我们。 Until you add an endl the output stream won't be flushed. 在添加endl之前,不会刷新输出流。

I would strongly suspect that you are running off the end of your positionsInParent1 or valuesInParent2 arrays. 我强烈怀疑您是在使用positionInParent1或valuesInParent2数组的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM