zoukankan      html  css  js  c++  java
  • 越界访问,调试真的很头疼

    访问以点(x,y)为起点的横纵10*10范围内的值,求和

    我先写了这个,怎么都不对

        for (int countY = 0 ; countY < 10 ;++countY,++y)
        {
            uchar *dst_ptr = (uchar *)(dst_gray->imageData + y * dst_gray->widthStep);
            for(int countX=0; countX < 10 ; ++countX,++x)
            {
                sum = sum + dst_ptr[x];
                cout<<(int)(dst_ptr[x])<<" ";
            }
            cout<<endl;
        }

    调试了2个小时,终于发现问题

    X会一直++下去而不会回到行头

    就好比是

    1111111111

    00000000001111111111

    000000000000000000001111111111

    0000000000000000000000000000001111111111

    00000000000000000000000000000000000000001111111111

    不用多久,X访问的位置就越界了

    正确的:

        for (int countY = 0 ; countY < 10 ;++countY,++y)
        {
            uchar *dst_ptr = (uchar *)(dst_gray->imageData + y * dst_gray->widthStep);
            for(int countX=0,xx=x; countX < 10 ; ++countX,++xx)
            {
                sum = sum + dst_ptr[xx];
                cout<<(int)(dst_ptr[xx])<<" ";
            }
            cout<<endl;
        }
  • 相关阅读:
    简单状态机
    c语言状态机
    存储公司
    正确跑步
    好好做自己能做的
    I2C学习
    es6 generator函数
    es6 for of 循环
    es6 proxy代理
    es6 Symbol类型
  • 原文地址:https://www.cnblogs.com/jun14/p/3330432.html
Copyright © 2011-2022 走看看