Stack cookie instrumentation code detected a stack-based buffer overrun - Fixed

Alex Braniff picture Alex Braniff · Feb 13, 2013 · Viewed 10.7k times · Source

so I am having a few issues with this program used to pixelate an image. One issue is that I get a "Stack around the variable 'pixArray' was corrupted" and then, when I click continue after breaking it gives the error in the title.

I'm not sure if it is acceptable to use pastebin, but I'll use it for the sake of having a "short" post.

The Code

The Image Being Used

Also, when it runs through, all of the pixelated squares are one pixel too short on the left and top of the squares. It is just using the original data when it writes to the outFile. If you could try to figure out why this is happening, you get bonus points.

Finally, the averages don't seem to be averaging correctly, as you can see by the squares around the right-most side of the image after running the program.

Any help with any of these problems would be greatly appreciated. Thanks in advance!

EDIT: I sorted through the code, commenting out sections that use pixArray, and the section that, when commented out, fixes the problem is at the bottom of the function, getAveragesForRGB

start = 0;//reset start number
for(int row = 0; row < squareSize; row++) {
    if(row != 0)
        start = ((square * MAXROWS) / (MAXCOLS / squareSize)) + 1;
    stop = (((square + 1) * MAXROWS) / (MAXCOLS / squareSize));
    for (int col = start; col < stop; col++) {
        //write each average into each piece of the array
        pixArray[row][col].red = redAvg;
        pixArray[row][col].green = greenAvg;
        pixArray[row][col].blue = blueAvg;
    }
}

EDIT 2: I got it all running smoothly now. Just in case anyone ever runs into this exact problem for whatever reason, here is the new getAveragesForRGB where all of my problems were.

void getAveragesForRGB(Pixel pixArray[][MAXCOLS], int squareSize, int square, int numSquaresPerStripe) {

    //initialize variables needed for function
    int start, stop;
    int redAvg, greenAvg, blueAvg;

    //reset averages for current square's usage
    redAvg = 0;
    greenAvg = 0;
    blueAvg = 0;

    start = 0; //reset start number
    for (int row=0; row < squareSize; row++) {
        if (row != 0)
            start = ((square * MAXROWS) / (MAXCOLS / squareSize)) - 1;  //starting point for loop over the columns
        stop = start + squareSize;//stopping point for   ^^^
        for (int col = start; col < stop - 1; col++) {
            //add each rgb value to the sum to be divided later
            redAvg += pixArray[row][col].red;
            greenAvg += pixArray[row][col].green;
            blueAvg += pixArray[row][col].blue;
        }
    }

    //divide by number of pixels in square for average
    redAvg /= (squareSize * squareSize);
    greenAvg /= (squareSize * squareSize);
    blueAvg /= (squareSize * squareSize);


    start = 0;//reset start number
    for (int row = 0; row < squareSize; row++) {
        if (row != 0)
            start = ((square * MAXROWS) / (MAXCOLS / squareSize)) - 1;  //starting point for loop over the columns
        stop = (((square + 1) * MAXROWS) / (MAXCOLS / squareSize));  //stopping point for   ^^^
        for (int col = start; col < stop - 1; col++) {
            //write each average into each piece of the array
            pixArray[row][col].red = redAvg;
            pixArray[row][col].green = greenAvg;
            pixArray[row][col].blue = blueAvg;
        }
    }
}

Answer