tags:

views:

188

answers:

4

I'm trying to learn C++ currently, but I'm having issues with the code below.

class Vector2
{
public:
    double X;
    double Y;

    Vector2(double X, double Y)
    {
        this->X = X;
        this->Y = Y;
    };

    SDL_Rect * getSdlOffset()
    {
        SDL_Rect * offset = new SDL_Rect();
        offset->x = this->X;
        offset->y = this->Y;
        return offset;
    };
};

Visual studio throws throw the following error when calling getSdlOffset()

An unhandled exception of type 'System.AccessViolationException' occurred in crossEchoTest.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I've got a C#/java background, and I'm lost... Any help would be much appreciated.

+1  A: 

You never initialized X or Y... what do you those values might be? More than likely they are point to 00000X00(I am rusty this may not be the right address, but you are pointed to memory outside of your programs allocated space... thus the "GPF" I was C/C++ "convert" to Java(over 11 years ago) so I can appreciate your ideas of how a pointer might behave--I can assure you that pointers are the most difficult part of C/C++ to understand, so you are on the right track in your learning. Just keep in mind that unlike Java/C#, C/C++ do not keep you from hurting yourself or the OS memory space/memory space of other programs. I alway remember what a teacher once told me when I was learning C--"With C you get a Kevlar boot and a gun, it is up to you whether or not you put the boot on before you shoot yourself in the foot, because you will shoot yourself at some point..." Good luck to you on learning C++, just hang in there and don't get discouraged.

WM

Wintermute
You were right! I had an instance of this Vector2 class that hadn't been initialized.Thanks for pointing me in the right direction!
PenguinBoy
A: 

Pretty sure Wintermute is correct. There was another SO question that got some great answers. You may want to look in there just to get some more background about what is going on:

http://stackoverflow.com/questions/1074894/accessviolationexception-in-release-mode-c

Hope it helps!

Morinar
Yes he was correct! I hadn't properly created one instance of the Vector2 class.
PenguinBoy
A: 

oops, language confusion, no SO for me when so sleepy.

msw
`this` is a pointer in C++.
indiv
A: 

There's really only two things can can go wrong in that function, either this is bad or offset is bad. Since you get offset from new SDL_Rect(); the only way that offset can be a bad pointer is if new fails but doesn't throw, which doesn't see very likely.

Thus we deduce that this is bad. Since you never show the code that you are using to allocate this, I'm going to guess that your code looks something like this.

  Vector2  * vec;
  vec->getSdlOffset(); // you crash here

You need to show us the code where you If you will show us the where you create the Vector2 object, we could maybe be more specific

John Knoeller