0
votes
class Register
{
private:
        DWORD ax,dx,cx,bx; // POH
        DWORD bp,sp;

        DWORD flag, ip;
public:
        //====================================================
        Register()
        {
             ax = 0x0;
             dx = 0x0;
             cx = 0x0;
             bx = 0x0;

            bp = 0x0;
          //memset(&this->sp,0,sizeof(sp));
            sp = 0x0;

            flag = 0x0;
            ip = 0x0;
        }
        //====================================================
        ~Register()
        {
        }
        //====================================================
        void setAx(DWORD d)
        {
         ax=d;
        }
        //====================================================
        DWORD getSp()
        {
          return sp;
        }

}*PReg;

Why does function getSp(); gives an Access Violation error?

1
the problem is not in this code but at the caller side. post the code - Karoly Horvath
void push(DWORD buf) { DWORD d = PReg->getSp(); stack[d]=buf; PReg->incSp(); } - Hakon89
it works here but when i do this : InstList->Lines->Add(PStack->pop()); // InstList - TMemo - Hakon89
You haven't shown the code for PStack and PReg->incSp. - Tommy Andersen

1 Answers

0
votes

You have forgotten to instantiate your class. You have made a variable that is a pointer to the class Register, but you have not instantiated it.

What you get is a pointer that is either null or pointing to some random memory location, and you are assuming that it is pointing to your instance of the class. So when you try to access any of the member variables your are actually accessing memory locations you do not have access to.

What you need to do is create a new instance of the class:

PReg = new Register();

May I at the same time suggest that you move the variable declaration away from the class prototype (which I assume resides in your header file).