访问违规是什么意思?

What does access violation mean?

本文关键字:意思 是什么 访问      更新时间:2023-10-16

我是c++的新手,不明白为什么我得到错误"访问违规读取位置"。下面是我的代码:

gdiscreen();
int startX = 1823 - minusX;
int startY = 915 - minusY;
for (int i = startX; i < startX + 61; i++)
{
    for (int j = startY; j < startY + 70; j++)
    {
        Color pixelColor;
        bitmap->GetPixel(i, j, &pixelColor);
        cout << pixelColor.GetValue() << " ";
    }
    cout << endl;
}

gdiscreen()可以在这里找到:http://forums.codeguru.com/showthread.php?476912-GDI-screenshot-save-to-JPG

访问冲突分段错误意味着您的程序试图访问未在作用域中保留的内存。
有几个例子如何实现这一点:

数组的左边界:

int arr[10];
for(unsigned char i=0; i<=10; i++)  //Will throw this error at i=10
    arr[i]=0;

注意:在上面的代码中,我使用unsigned char进行迭代。Char是一个字节,所以unsigned char是0-255。对于较大的数字,您可能需要unsigned short(2字节)或unsigned int(4字节)。

不小心使用指针而不是整数进行计算

int ah = 10;
int *pointer = &ah;   //For some reason, we need pointer
pointer++;   //We should've written this: (*pointer)++ to iterate value, not the pointer
std::cout<<"My number:"<<*pointer<<'n';  //Error - accessing ints address+1

我故意以宽泛的解释开始。你一开始就想知道什么是访问侵犯。在你的特定代码,我很确定你搞砸了ij的边界。做一些std::cout调试