I am writing a program that should create random sized green boxes at random locations in a 640x480 window. I am getting a segmentation fault when I run the following code. The problem resides in the two "for" loops. The segfault usually occurs in the nested "for" loop with startx. I suspect a buffer overflow, but don't know how to make less bulky code.
//Globals
int width, height;
int endx, endy, starty, startx, randEnd, randStartX, randStartY;
unsigned char *pixmap;
void setPixels(){
for (int j = 1; j<100; j++) { // j == j-1 # of boxes
randStartX = rand() % width; // random # btw 0 and width
randStartY = rand() % height; // random # btw 0 and height
randEnd = 1 + (rand() % 100); // random # btw 0 - 100, not allowing box > 100.
startx = randStartX;
starty = randStartY;
endx = startx + randEnd;
endy = starty + randEnd;
for(int y = starty; y < endy; y++) { // first y coordinate of box
for(int x = startx; x < endx; x++) { // first x coordinate of box
cout << "endx = " << endx << endl;
int i = (y * width + x) * 3; // movement upwards for each pixel
pixmap[i++] = 0x00; //Increments i by one to move to the next part of pixel.
pixmap[i++] = 0xFF;
pixmap[i] = 0x00;
}
}
}
}
int main(int argc, char *argv[])
{
//initialize the global variables
srand (time(0));
width = 640;
height = 480;
pixmap = new unsigned char[width * height * 3];
setPixels(); // write code like ./pr01 red, etc.
glutInit(&argc, argv);
glutInitWindowPosition(100, 100); // Where the window will display on-screen.
glutInitWindowSize(width, height);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Assignment 01");
init();
glutReshapeFunc(windowResize);
glutDisplayFunc(windowDisplay);
glutMouseFunc(handleButton);
glutMainLoop();
return 0;
}
Any idea of what could be causing this? Are there any blatant logic issues here? Thanks in advance.
How to look at this problem is assume the randStartX
was set to 639 and randStartY
to 479. Now you say find a random number to determine size of box (max 100). If you start from bottom right corner you can't create any box beyond the array bounds. your randEnd
code has to account for boxes that exceed the bounds when added to randStartX
and randStartY
. randEnd needs to be constrained or in your 2 for loops you need to make sure that you constrain writing beyond the edge of the display area (pixmap).
The best way is to constrain endx
and endy
. You can do this and fix your bug by replacing
endx = startx + randEnd;
endy = starty + randEnd;
with:
endx = min(startx + randEnd, width-1);
endy = min(starty + randEnd, height-1);
Use the min
function to limit the box so it doesn't extend beyond the edge of width
and height
(subtract 1 since we are 0 based)