简体   繁体   English

C ++访问冲突写入位置0x00000000

[英]c++ Access violation writing location 0x00000000

I have the following code: In Client.cpp there is the constructor that is doing memory allocation for the array "conturi" of type ContBancar. 我有以下代码:在Client.cpp中,有一个构造函数正在为ContBancar类型的数组“ conturi”进行内存分配。

Client::Client(string nume, string prenume, string adresa)
{
    this->nume = nume;
    this->prenume = prenume;
    this->adresa = adresa;
    nrCont = 0;
    ContBancar** conturi = new ContBancar*[50]; 
}

Then there is a method that is adding a account in the "conturi" array: 然后,有一种方法可以在“ conturi”数组中添加一个帐户:

void Client::adaugaCont(ContBancar* contNou)
{
    conturi[nrCont++] = contNou;
}

This is my code in Main.cpp: 这是我在Main.cpp中的代码:

ContBancar ron1 ("1.r", 0, Banca::RON);
Client ioana ("Pop", "Ioana", "Str Dorobantilor 3/4");
ioana.adaugaCont(&ron1);

But it gives me the access violation error at runtime, like the array 'conturi' has no memory allocated. 但这给了我运行时访问冲突错误,就像数组“ conturi”没有分配内存。 I don't understand why, because the memory should be allocated in the constructor. 我不明白为什么,因为内存应该在构造函数中分配。

Could anyone help me with this? 有人可以帮我吗?

Client::Client(string nume, string prenume, string adresa)
{
    this->nume = nume;
    this->prenume = prenume;
    this->adresa = adresa;
    nrCont = 0;
    //here is your problem!
    ContBancar** conturi = new ContBancar*[50]; 
}

You redefine conturi as a new array, with the pointer to it stored in the local scope of the constructor. 将conturi重新定义为新数组,并将其指针存储在构造函数的本地范围中。

Change the line to: 将行更改为:

conturi = new ContBancar*[50]; 

and you will then have the object's conturi pointer pointing to the allocated memory. 然后,您将使对象的conturi指针指向分配的内存。
This will also solve the memory leak you have introduced. 这也将解决您引入的内存泄漏。
(pointer to heap goes out of scope. memory on heap is leaked) (指向堆的指针超出范围。堆上的内存泄漏)

Or even better, use a std::vector. 甚至更好,请使用std :: vector。
In Class definition: 在类定义中:

std::vector<ContBancar> conturi;

You don't have to manage the memory yourself with new and delete and you are not restricted to a fixed number of elements either. 您不必自己使用newdelete管理内存,并且您也不限于固定数目的元素。

You're declaring a new pointer variable with the following line: 您在下面的行中声明了一个新的指针变量:

ContBancar** conturi = new ContBancar*[50]; 

and the pointer variable will get destroyed at the end of the function call and leak memory leaving any other same-name member variable untouched. 并且指针变量将在函数调用和泄漏内存结束时被销毁,而其他任何同名成员变量都将保持不变。

You should rather use the member variable conturi (assuming you have one from the rest of the code): 您应该使用成员变量conturi (假设您在其余的代码中有一个):

Client::Client(string nume, string prenume, string adresa)
{
    this->nume = nume;
    this->prenume = prenume;
    this->adresa = adresa;
    nrCont = 0;
    conturi = new ContBancar*[50];
}

or you can use a std::vector<ContBancar> conturi member variable which might be easier to use and to deal with. 或者您可以使用std::vector<ContBancar> conturi成员变量,该变量可能更易于使用和处理。

This is a simple reproducer to better understand the problem: 这是一个简单的复制器,可以更好地理解问题:

class Client {
public:
    int** conturi = 0;

    void function() {
        int** conturi = new int*[50];
    }
};

int main()
{
    Client obj;
    obj.function();
    if(obj.conturi == 0)
       std::cout << "this is still zero"; // This will get printed
}

Example

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

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