Question

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;
     }   
    }
Était-ce utile?

La solution

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++)
{
    ...
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top