尝试向指针添加内存时程序崩溃

时间:2018-01-08 10:25:18

标签: c++ crash

我有这个代码,并且在访问班级" portofel"它崩溃了。 使用调试器,它表明它在尝试从构造函数中读取char * moneda 时无法读取内存,但它读取的属性来自类" Operatie&# 34;这是" Portofel"

的父母
class Operatie 
{
    private:
    char *moneda;


    void set_moneda(char* moneda_noua)
    {
        if (moneda != NULL)
        {
            delete[]this->moneda;
        }
        this->moneda = new char[strlen(moneda_noua) + 1]; //*debugger says "unable to read memory"*
        strcpy(moneda, moneda_noua);
    }
}

int main()
{
    Buget b;
    char characterIntrodus;
    Operatie o; //this is the point where it goes to check the function

    return 0;
}

抛出的异常是:Access违规读取位置抛出异常

我尝试更新代码以便能够编译,但是当我只使用属性和类的代码时,它可以工作。所以我想这不仅仅是关于这门课程吗?我在第一行解码时放了一个屏幕截图。enter image description here

0 个答案:

没有答案