user1251858
user1251858

Reputation: 313

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...

Upvotes: 0

Views: 1586

Answers (1)

RobH
RobH

Reputation: 3338

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.

Upvotes: 3

Related Questions