Segmentation Fault 11 Redux

Autex

我正在编写一个程序,该程序应在640x480窗口中的任意位置创建随机大小的绿色框​​。运行以下代码时出现分段错误。问题出在两个“ for”循环中。段错误通常发生在带有startx的嵌套“ for”循环中。我怀疑缓冲区溢出,但是不知道如何减少笨重的代码。

//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; 
}

知道是什么原因造成的吗?这里有任何公然的逻辑问题吗?提前致谢。

迈克尔·佩奇

如何看待这个问题是假设randStartX设置为639和randStartY479。现在,您说找到一个随机数来确定盒子的大小(最大100)。如果从右下角开始,则无法创建超出数组范围的任何框。randEnd添加到randStartX时,您的代码必须考虑超出范围的盒子randStartYrandEnd需要被约束,或者在您的2 for循环中,您需要确保将书写限制在显示区域(pixmap)的边缘之外。

最好的方法是约束endx约束endy您可以执行此操作并通过替换来修复错误

endx = startx + randEnd;
endy = starty + randEnd;

和:

endx = min(startx + randEnd, width-1);
endy = min(starty + randEnd, height-1);

使用min函数限制框,使其不超出widthand的边缘height(由于我们从0开始,所以减去1)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Xcode-编译Swift Segmentation Fault 11

来自分类Dev

iOS Xcode compile error: unable to execute command: Segmentation fault: 11

来自分类Dev

Segmentation fault: 11 in a program of a C in a multithread using pthread library

来自分类Dev

segmentation fault (core dump)

来自分类Dev

Qt Segmentation Fault strtod

来自分类Dev

C ++ 11 Segmentation Fault尝试将数组(<algorithm>)动态复制到向量中

来自分类Dev

Segmentation Fault 11链接OS X 32位汇编器

来自分类Dev

在Xcode中用枚举编译swift文件时出现Segmentation Fault 11

来自分类Dev

我在C ++中更改当前目录的代码不断给我带来Segmentation Fault:11错误

来自分类Dev

Segmentation fault (core dumped) with Threads

来自分类Dev

Cassandra: cqlsh segmentation fault on mac

来自分类Dev

Segmentation Fault中“代码”的含义是什么

来自分类Dev

segmentation fault in during dynamic memory allocation with malloc

来自分类Dev

Segmentation fault (core dumped) if variable inside main?

来自分类Dev

Cucumber Test with Frank on Yosemite result in Segmentation Fault

来自分类Dev

在packageRunBuild Segmentation Fault上Gradle构建失败

来自分类Dev

用Segmentation Fault填充结构的数组崩溃

来自分类Dev

python Segmentation Fault(核心已转储)

来自分类Dev

调试期望脚本时出现“ Segmentation Fault”

来自分类Dev

c++ 出现奇怪的Segmentation fault

来自分类Dev

给出 Segmentation fault 和 idk where

来自分类Dev

为什么 FFmpeg 会突然抛出“Segmentation fault: 11”错误并创建仅包含“ftypisomiiso2avc1mp4freemdat”作为内容的输出文件?

来自分类Dev

shout-python segmentation fault我该如何解决?

来自分类Dev

Segmentation fault on printf - NASM 64bit Linux

来自分类Dev

Recursive quick-sort causing segmentation fault (not overflow)

来自分类Dev

QtSerialPort has unavailable data, segmentation fault under Windows 8 only

来自分类Dev

Android在设备上安装APK会显示[SEGMENTATION FAULT]

来自分类Dev

在结构中输入字符串:segmentation fault

来自分类Dev

NASM x86_64 scanf segmentation fault

Related 相关文章

  1. 1

    Xcode-编译Swift Segmentation Fault 11

  2. 2

    iOS Xcode compile error: unable to execute command: Segmentation fault: 11

  3. 3

    Segmentation fault: 11 in a program of a C in a multithread using pthread library

  4. 4

    segmentation fault (core dump)

  5. 5

    Qt Segmentation Fault strtod

  6. 6

    C ++ 11 Segmentation Fault尝试将数组(<algorithm>)动态复制到向量中

  7. 7

    Segmentation Fault 11链接OS X 32位汇编器

  8. 8

    在Xcode中用枚举编译swift文件时出现Segmentation Fault 11

  9. 9

    我在C ++中更改当前目录的代码不断给我带来Segmentation Fault:11错误

  10. 10

    Segmentation fault (core dumped) with Threads

  11. 11

    Cassandra: cqlsh segmentation fault on mac

  12. 12

    Segmentation Fault中“代码”的含义是什么

  13. 13

    segmentation fault in during dynamic memory allocation with malloc

  14. 14

    Segmentation fault (core dumped) if variable inside main?

  15. 15

    Cucumber Test with Frank on Yosemite result in Segmentation Fault

  16. 16

    在packageRunBuild Segmentation Fault上Gradle构建失败

  17. 17

    用Segmentation Fault填充结构的数组崩溃

  18. 18

    python Segmentation Fault(核心已转储)

  19. 19

    调试期望脚本时出现“ Segmentation Fault”

  20. 20

    c++ 出现奇怪的Segmentation fault

  21. 21

    给出 Segmentation fault 和 idk where

  22. 22

    为什么 FFmpeg 会突然抛出“Segmentation fault: 11”错误并创建仅包含“ftypisomiiso2avc1mp4freemdat”作为内容的输出文件?

  23. 23

    shout-python segmentation fault我该如何解决?

  24. 24

    Segmentation fault on printf - NASM 64bit Linux

  25. 25

    Recursive quick-sort causing segmentation fault (not overflow)

  26. 26

    QtSerialPort has unavailable data, segmentation fault under Windows 8 only

  27. 27

    Android在设备上安装APK会显示[SEGMENTATION FAULT]

  28. 28

    在结构中输入字符串:segmentation fault

  29. 29

    NASM x86_64 scanf segmentation fault

热门标签

归档