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 🙂
This is why (as far as I can tell given what you posted)
PRegis 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 tonewit 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.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.
Also, I have no idea why you are using
memsetto initializesp.