welcome guest
login or register

Add new reply

Now this one was tricky!

My code crashed with a segmentation fault - it appeared to be somewhat random; sometimes I could run my algorithm 1000 times all fine, and sometimes it crashed on 400th run. And a segfault is typically caused somewhere earlier in the code, so you can't find the cause by spotting the crashing point. I had to experiment with commenting out different parts of the code until I got it narrowed down to a single line:
weatheryear[i].temp=currtemp+0.5;

At first I thought that i is somehow running outside the array, but no - it was always within 0..364 (I'm precalculating a temperature value for each day of a year). Then what? currtemp is a float. And temp is a signed char, which is enough to store values ranging from -50 ... +50. And currtemp is always inside this range,too. Can you spot the bug?

Well, I don't know if this is documented somewhere, but this was remedied by changing temp to be a signed int. So, somehow, in some cases, converting a float to a signed char causes a memory leak. I didn't know that!

CAPTCHA
Please reply with a single word.
Fill in the blank.