访问冲突是什么意思? [英] What does access violation mean?

查看:599
本文介绍了访问冲突是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,不明白为什么会收到错误消息 Access Violation Reading Location。这是我的代码:

I'm new to C++ and do not understand why I am getting the error "Access Violation Reading Location". Here is my code:

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屏幕快照保存到JPG

gdiscreen() can be found here: http://forums.codeguru.com/showthread.php?476912-GDI-screenshot-save-to-JPG

推荐答案

访问冲突 segmentation fault (分段错误)意味着您的程序试图访问未在范围内保留的内存。

下面是一些实现此目的的示例:

Access violation or segmentation fault means that your program tried to access a memory that was not reserved in the scope.
Have a few examples how to achieve this:

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个字节)。

Note: In the code above, I use unsigned char to iterate. Char is one byte, so unsigned char is 0-255. For larger numbers, you may need unsigned short (2 bytes) or unsigned int (4 bytes).

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

我有意从宽泛开始说明。您想首先了解什么是访问冲突。在您的特定代码中,我非常确定您混淆了 i j 边界。做一些 std :: cout 调试。

I intentionally started with broad explanation. You wanted to know what access violation is at the first place. In your particular code, I'm very sure you messed up with i and j boundaries. Do some std::cout debug.

这篇关于访问冲突是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆