访问冲突是什么意思? [英] What does access violation mean?
问题描述
我是 C++ 新手,不明白为什么会收到错误访问冲突读取位置".这是我的代码:
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-screenshot-save-to-JPG
gdiscreen() can be found here: http://forums.codeguru.com/showthread.php?476912-GDI-screenshot-save-to-JPG
推荐答案
访问冲突 或 分段错误 意味着您的程序试图访问未保留的内存范围.
举几个例子来说明如何实现这一点:
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<<'
'; //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屋!