简体   繁体   English

违反内存访问C ++ Borland

[英]Memory Access violation C++ Borland

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(); 为什么当我使用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 "????" 它给了我一个AV错误,我在初始化该点时跟踪了该变量,并给了我一个设置为0的随机数,将其设置为零,在问题函数处,sp变量为“ ????”。 ?

Register *PReg; - PReg :) -PReg :)

This is why (as far as I can tell given what you posted) 这就是为什么(据我所知您所发布的内容)

Register *PReg;

PReg is not valid. PReg无效。 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. 您需要new它或使用其他地方的有效指针对其进行初始化。 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. 坦白说,除非您知道自己在做什么,否则不应在C ++中以这种方式设计代码。 Look into RAII . 调查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 . 另外,我也不知道为什么要使用memset初始化sp

The code compiles and runs correctly, so it is most likely in the way you're instantiating the PReg variable. 该代码可以正确编译并运行,因此很可能是您实例化PReg变量的方式。 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. 并且都按预期打印“ 0”。 If you could post up how you're creating the PReg instance, that would be helpful. 如果您可以发布如何创建PReg实例,那将很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM