0

I would be most grateful if people could have a look over this snippet of code and let me know what could a possible cause for the floating point exception.

Info:

  • branches is an int array size 200
  • line is a char array size 20
  • The loop runs fine 6 times, then the exception occurs.

I am confused because there is no division, float or integer, that could cause this.

    for (count = 0; count < sizeof(branches); count++){

    if (fgets(line,sizeof(line),fp)==NULL)
     break;
    else {

    int branch_taken = line[16] - 48; 

    branches[count] = branch_taken;
     }   
    }
franka
  • 1,867
  • 3
  • 17
  • 31
  • On what line is the exception thrown? Could it be that the loop is exited and the exception thrown elsewhere? – eggyal Oct 23 '12 at 22:50

1 Answers1

7

sizeof(branches) is a size in bytes - you need to use a constant which represents the number of elements i.e. 200, otherwise you will be exceeding the bounds of your branches array, which will result in Undefined Behaviour.

Your code should probably look something like this:

#define NUM_BRANCHES 200

int branches[NUM_BRANCHES];

for (count = 0; count < NUM_BRANCHES; count++)
{
    ...
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    @Nick: ITYM `sizeof(branches) / sizeof(branches[0])` ? – Paul R Oct 22 '12 at 18:39
  • Thank you so much; would have never realized this. – franka Oct 22 '12 at 18:40
  • @DanielFischer Well, there's UB due to out of bounds access. So you can take FPE as a reason :P – P.P Oct 22 '12 at 18:41
  • @Daniel Fischer: once you have UB then then no further explanation is required. – Paul R Oct 22 '12 at 18:41
  • 1
    I bet the code later on divides by the input value. Since it's out-of-bounds, it's probably getting overwritten with a zero. – Mysticial Oct 22 '12 at 18:42
  • @DanielFischer It doesn't, but that's why the loop stops randomly after 6 iterations even though the FPE is located somewhere else in the program. – franka Oct 22 '12 at 18:42
  • @KingsIndian If the exception occurs after `count` has become `>= 200` (as I suspect it does), then yes, UB (but of course, it's still interesting if a compiler chooses an unusual way). But if it really occurs after six iterations, that's too early. – Daniel Fischer Oct 22 '12 at 18:43
  • @FSchmidt If the loop stops after six iterations, the cause must be inside the loop (unless the compiler writers went out of their way to detect that the programme will invoke UB and decided to have a bit of fun by crashing earlier - or you had previous UB). Probably an integer division by 0. – Daniel Fischer Oct 22 '12 at 18:47
  • Found the division by 0, (way) outside of the loop. I guess that means the compiler writers had fun, then. ^^ – franka Oct 22 '12 at 18:51
  • @FSchmidt Or I have misinterpreted your "the loop runs fine six times". If that means the loop runs to completion six times, you had UB already during the first run. I thought it meant it runs fine for `count = 0 .. 5` and crashes when `count == 6`, but now I tend to think the first interpretation is the case. – Daniel Fischer Oct 22 '12 at 19:07