Search code examples
c++arraysbus-error

Bus Error:10 with more indexes to go


I'm using 6 arrays of ints, each holding 256 elements. Pretty standard, I'd think.

The problem I'm facing is while I'm initialising all these arrays to be full of 0s, I'm getting a Bus Error:10. Each time in the 240th iteration of my loop.

Here's my header file...

class histEqImage {
private:
int histogramR[256];
int histogramG[256];
int histogramB[256];

int dashR[256];
int dashB[256];
int dashG[256];

void initHistograms();    
public:
//Other declarations here...
};

And here's the function that the problem is occurring in...

void histEqImage::initHistograms() {
//Ensure all the values in the histogram are at 0.
for(i = 0; i <256; i++) {
    histogramR[i]= 0;
    histogramG[i] = 0;
    histogramB[i] = 0;
    dashR[i] = 0;
    dashG[i] = 0;
    dashB[i] = 0;
}
}

And so each time I get to this part of my code, the program crashes, at i=240. Sorry, to specify a bit better, it happens in the dashB[i] = 0; line

It's vexing me to no end, I can't see where I'm going out of bounds of my array, and I'm not messing around with powers far beyond my comprehension in terms of dynamic memory.

Any help, would be helpful.


Solution

  • Since the size of histEqImage pictureDisplay is 6 KB (assuming 32-bit ints), it might be too big for allocation on the stack. Try moving it outside your main, or allocate it dynamically.

    If you move the declaration outside main, make it static to avoid giving it global visibility.

    static histEqImage pictureDisplay;
    
    int main(int argc, char *argv[]) {
        //...
    }