Never knew C++ sort can give a Segmentation Fault

I never knew such a thing could happen, and I learnt it the hard way.

I was trying my luck at the Facebook Hacker Cup, completed a question, ran the given sample test case (which was so long that had to scroll down a lot of times) and the output turned out to be fine. I decided to download the final test case (For the ones unaware of the hacker cup format, I would say that its different from the usual judges. You code, then download the test case and within 6 minutes you need to upload the test case output). The timer started and as soon as I ran my code on the file....BANG!!! seg fault. It took me a few seconds to figure out the reason for it and I realized it was at the C++ sort function call. With no idea in mind I desperately tried to correct it, but could not do so. The timer expired and so did my enthusiasm for doing any further questions. And believe me if such a thing happens while coding at 4:00 am, it hurts!!!.

Finally it turned out, the mistake was my poorly coded sort comparator function. It made a situation of kind: a<b, b<c, c<a, possible.

Fool, ain't I.

No comments:

Post a Comment