2
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;
        }
        //====================================================
        void setDx(DWORD d)
        {
         dx=d;
        }
        //====================================================
        void setCx(DWORD d)
        {
         cx=d;
        }
        //====================================================
        void setBx(DWORD d)
        {
         bx=d;
        }
        //====================================================
        void setBp(DWORD d)
        {
         bp=d;
        }
        //====================================================
        void incSp()
        {
         sp = sp+1;
        }
        void decSp()
        {
        if(sp == 0)
        {
        sp = 0;
        }
        sp = sp - 1;
        }
        //====================================================
        void setFlag(DWORD d)
        {
        flag=d;
        }
        //====================================================
        void setIp(DWORD d)
        {
         ip=d;
        }
        //====================================================
        DWORD getAx()
        {
        return ax;
        }
        //====================================================
        DWORD getDx()
        {
        return dx;
        }
        //====================================================
        DWORD getCx()
        {
        return cx;
        }
        //====================================================
        DWORD getBx()
        {
        return bx;
        }
        //====================================================
        DWORD getBp()
        {
        return bp;
        }
        //====================================================
        DWORD getSp()
        {
          return this->sp;
        }
        //====================================================
        DWORD getFlag()
        {
        return flag;
        }
        //====================================================
        DWORD getIp()
        {
        return ip;
        }
        //====================================================
};

Why when i use getSp(); function like this:

PReg->getSp();

it gives me an AV error, I traced this variable at the point where I initialize it gives me a random number insted of zero, which I set to and at the point of problem function the sp variable is "????" ?

Register *PReg; - PReg :)

2
Why are you treating SP differently from the other ones? How are you allocating your PReg? - Mat
Can you show us the code that declares PReg? - 逆さま
How are you declaring your instance of Register? - Chad
No one can help me out ? - Hakon89
Are you actually assigning a value to your pointer to Register PReg? If you aren't then it is garbage and would likely result in an access violation. - Michael Price

2 Answers

0
votes

This is why (as far as I can tell given what you posted)

Register *PReg;

PReg is not valid. Sure, you declare it, but you never initialize it, so its value is indeterminate and dereferencing it results in undefined behavior. You need to new it or initialize it with a valid pointer from somewhere else. Simply declaring it does not allocate memory and initialize your pointer. The same goes for any variable that does not have static storage space, but even if it were static, a pointer would still not be valid unless it is initialized somewhere before being dereferenced.

Register *PReg = new Register();
// use it...
delete PReg;

Honestly though, you shouldn't design your code this way in C++ unless you know what you are doing. Look into RAII. As for your example, I see no reason why you wouldn't just use automatic storage.

void Foo() {
    Register reg;
    // use reg
}  // reg goes out of scope, memory reclaimed

Also, I have no idea why you are using memset to initialize sp.

0
votes

The code compiles and runs correctly, so it is most likely in the way you're instantiating the PReg variable. I've done it two ways, the first is the way I assume you're doing it:

Register *PReg = new Register;
cout << PReg->getSp() << endl;

And also just declaring it:

Register PReg;
cout << PReg.getSp() << endl;

And both print "0" as expected. If you could post up how you're creating the PReg instance, that would be helpful.