Bus Error Without Direct Addressing

Hi all,

I'm getting a bus error when calling the function below with a taxableIncome < taxCap[cod'e'][0]. (EX: calcTaxAmt(1, 3000);)

Offending function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
float calcTaxAmt(int code, float taxableIncome) {
    float tax = 0.0;
    float taxRate[6] = { .12, .16, .2, .28, .32, .4 };
    float taxCaps[4][5] = { { 7550.0, 30650.0, 74200.0, 154800.0, 336550.0 },
                            { 15100.0, 61300.0, 123700.0, 188450.0, 336550.0 },
                            { 7550.0, 30650.0, 61850.0, 94225.0, 168275.0 },
                            { 10750.0, 41050.0, 106000.0, 171650.0, 336550.0 } };
    code--; // Compensate for use as index (0-based counting)

     { // Anonymous namespace to hide i

        unsigned int i = 0;
        for( ; i < 5 && taxableIncome >= taxCaps[code][i]; i++) { // Add up all but the last tax brackets
            if(i == 0) {
                tax += taxCaps[code][i] * taxRate[i];
            } else {
                tax += (taxCaps[code][i] - taxCaps[code][i - 1]) * taxRate[i]; 
            } // END if/else
        } // END for(i)

        if(i == 5) { // CASE : (i < 5) broke the loop
            tax += (taxableIncome - taxCaps[code][4]) * taxRate[5];
        } else { // CASE : (taxableIncome >= taxCaps[code][i]) broke the loop
            tax += (taxableIncome - taxCaps[code][i - 1]) * taxRate[i];
        } // if/else

    }  // END Anonymous namespace

return tax;
} // END calcTaxAmt(code, income) 


What am I missing?

EDIT: Annoyingly named variable 'code' being used as an index made some unintended formatting l0l

EDIT 2: http://stackoverflow.com/questions/212466/what-is-a-bus-error has a comment that says "I was getting a bus error when root was at 100%". Coincidentally, my /boot dir is completely full; is that what is causing it? I don't have access to another computer to test this atm =[
Last edited on
In the example call you gave (i.e., calcTaxAmt(1, 3000);), the loop never executes. Since i is still zero, the next thing you do is attempt to access the array element at offset -1 on line 24.
Topic archived. No new replies allowed.