简体   繁体   中英

segmentation fault when ENTERING loop

I have a seg fault in C++ when entering a for loop. 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...

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. Until you add an endl the output stream won't be flushed.

I would strongly suspect that you are running off the end of your positionsInParent1 or valuesInParent2 arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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