Segmentation Fault in running Simulation

Requests regarding how to set up experiments in ARGoS.
Waqar731
Posts: 65
Joined: Thu Oct 23, 2014 12:33 pm
Location: Pakistan
Contact:

Segmentation Fault in running Simulation

Postby Waqar731 » Sun Feb 15, 2015 9:46 pm

Hello,
I have surrounded an arena of 10x10 boxes in Argos2 simulator.

According to arena, i made an array of [10]X[10]. I write code for declaration of array in my header file as private attribute.

Code: Select all

Real DataStorageArray[10][10];
Then i iterate an array in my control step with code.

Code: Select all

for( int w=-2;w<=6;w++) { if(w==0) { for( int x=4;x>=-4;x--) { if(x==-1) { DataStorageArray[-0][-0]=0.0f; printf("%s%f"," ", DataStorageArray[-0][-0]); } DataStorageArray[-0][x]=0.0f; printf("%s%f"," ", DataStorageArray[-0][x]); } printf("\n\n\n"); } for( int y=4;y>=-4;y--) { if(y==-1) { DataStorageArray[w][-0]=0.0f; printf("%s%f"," ", DataStorageArray[w][-0]); } DataStorageArray[w][y]=0.0f; printf("%s%f"," ", DataStorageArray[w][y]); } printf("\n\n\n"); }

When i start simulation step by step, then after first step, i get the following output. And this is my correct output as i code.

Code: Select all

0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

But when i press button for next step from Argos Simulator, then i get the following error.

Segmentation fault (core dumped)


What is the reason behind this? If there had been an error in my code, then it shouldn't work even on first step.
Why it is showing an error while on second step and why it is giving correct output first time?


Thanks
Waqar Hussain

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Segmentation Fault in running Simulation

Postby pincy » Mon Feb 16, 2015 5:13 am

Hi,

In your code:

Code: Select all

DataStorageArray[w][-0]=0.0f; ... DataStorageArray[w][y]=0.0f;
These commands could be executed with values of w and y that exceed the array boundaries (e.g., the negative values in the for loops).

This means that you would write portions of memory that you shouldn't write. Memory corruption causes segmentation faults.

On why exactly the first time it works and the second it doesn't, I don't know. But the code, as it's written, is incorrect.

Cheers,
Carlo
I made ARGoS.


Return to “How to... ?”