SIGSEGV while executing LOCALLY but AC on online judge

This happened a while back, I got Segmentation Fault when I ran the code for DIVMAC locally on my console but it worked perfectly on ideone and also got an AC on submitting. Can anyone tell why the anomaly?

The AC solution link

https://www.codechef.com/viewsolution/11533907

PS: The question is asked because many people on the community discourage people from using Ideone and advise people to run their code locally but what if we face issues like this?

You should see the constraints maybe there’s no case out of bounds.

1 Like

I am just upvoting you so as you can ask your questions.

@coder_voder thank you very much

@only4 there was no case of out of bounds, it’s just that why does the code run properly on ideone but not on my machine when I’m using the same language and the compiler version to build and execute my program at both the places.

You say that your code got AC on codechef but gets Seg. Fault on pc?

There is a reason for it. It depends on the online judge and how he handles exceptions.

For example-

Look at following code-

 int arr[10];
 cout<< arr[1];
cout<< arr[11];

Now, the output of this code would vary from one compiler to error. One compiler may throw error at “cout<< arr[11];” as here we are going out of bounds, other compiler may assign garbage values to both, arr[1] and arr[11] and print those.

And compilers of sites like hackerrank are so polite, they don’t tell you if anythings wrong or not, they just assign it 0 (and make debugging a hell of another level sometimes XD). Meaning, it would print 0 for arr[1] and arr[11].

It depends on how the compiler handles these things. Some very common errors for Seg. Fault/ Runtime errors on pc are-

int j,arr[n];
if(arr[j]==1)
{
...
}

Compilers may again differ here, since j isn’t initialised. By default (generally pc compilers), garbage value should be given and the line arr[j]==1 should throw an error (as garbage values are usually out of index/can be negative &etc.)

However, as I said, some compilers would assign j a initial value of 0, then since arr is also not initialised, they would give arr[j] also a default value of 0. And for them, your code would run without error, (and perhaps print desired output if you intended arr[j] =0).

Generally online compilers have exceptional handling, so they tolerate minor errors from our part. That’s my personal opinion of why your code ran on codechef but gave error on pc.

Also, you raised a point as such where to write codes if PC compilers give such errors. You can use code, compile and run feature of codechef, or go to any site like hackerrank, hackerearth etc. , open any practice problem and start coding your code there. Then to test it, use the option of “Test against custom input”. So you can exploit the benefits of that online compiler without any hassle.

(PS: Yes, I exploit this more times than I would like to admit.)

1 Like