What does access violation mean?
Asked Answered
H

1

3

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() can be found here: http://forums.codeguru.com/showthread.php?476912-GDI-screenshot-save-to-JPG

Honeysuckle answered 17/4, 2013 at 23:4 Comment(6)
does it print anything out? or straight away fails? where's bitmap come from? sure its initialized properly?Mariselamarish
There are even more questions. What is minusX and minusY? Did you check whether you are accessing with valid i and j coordinates?Terrazzo
What "access violation" means precisely is not so important; what matters is that it means that your program is incorrect and needs to be debugged.Hypanthium
It means you're doing something wrong with memory. Most likely bitmap is not what you think it is. Do some debugging. Make a testcase.Ruck
It doesn't print anything, and my test case outside of the for loop fails. I used the same code in the link but made bitmap a global pointer variable (I'm used to java/C#). Any more ideas?Honeysuckle
Soooo many magic numbersShang
S
5

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:

Leaving bounds of array:

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

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).

Accidentally calculating with pointer instead of integer

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 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.

Seligman answered 17/4, 2013 at 23:5 Comment(5)
Exceeding the bounds of an array will not cause AVs/SFs when the array is on the stack, and is not guaranteed when the array is on the heap, either. And your int pointer example is similar, because the incremented pointer is accessing stack space, so it won't cause an AV/SF either. Pick a better example, like dereferencing a NULL pointer (or a near-NULL pointer)Plutocrat
Isn't unsigned char supposed to be unsigned int.Barrel
@0x499602D2: loop counters can use any numeric data type. Since the array in the example only has 10 elements, the loop is merely using the smallest data type available.Plutocrat
This seems rather tangential to the questionMiley
I don't think it's tangential, OP is asking what causes the error. And it is simply reading/writing invalid memory locations. The error may not be immediate, it may take a while for it to even become evident when the error occurs. For instance a function like std::string foo(bool which) { if (which) return "Hi!"; }, while strictly valid, will cause stack and/or heap corruption. And you'll crash no where near the call to foo, when you call foo(false).Omega

© 2022 - 2024 — McMap. All rights reserved.