Shallow copy process of C++ copy constructor?

Asked 2 years ago, Updated 2 years ago, 110 views

//

#include "stdafx.h"
#include <iostream>
using namespace std;

class MyClass
{
public:
    MyClass()
    {
        pnumber = new int(5);
    }

    ~MyClass()
    {
        delete pnumber;
    }

    int number = 0;
    int *pnumber = nullptr;
};

int main()
{

    MyClass a;
    a.number = 300;
    cout << "value of a.n" << a.number << endl;
    cout << "a.Value of pn" << *a.pnumber << endl;
    cout << "a.Address value of pn" <<a.pnumber << endl;

    MyClass b(a);
    cout << "b.The value of n" <<b.number << endl;
    cout << "b.The value of pn" << *b.pnumber << endl;
    cout << "b.Address value of pn" <<b.pnumber << endl;

    a.number = 400;

    return 0;
}

I'm still an undergraduate, so I don't know exactly how to ask questions, so I apologize for the difficulty with the title and questions.

In the above code, instance b copies a through the default copy constructor, which I learned is

Because the default copy generator is a shallow copy machine, b as shown in the figure.The pnumber variable is a.It is copied in the form of pointing to new int (5) indicated by pnumber.

At this time, since the destructor contains the delete number, it is like releasing the already released memory (red arrow) again, resulting in an error.

That's what I know.

If you're curious about this,

a.pnumber is explicitly assigned by the constructor's pnumber = new int(5) code, b. generated by the copy constructor.Does number really get assigned (before it becomes a copy generator as shown)? If so, how and when will you receive it? It's about.

When an error occurs

(1) b.pnumber is assigned new int(5) (red arrow) -> Memory (red arrow) release due to shallow copy -> Destructor code requests release again for already released area, resulting in error

(2) b.The pnumber is a upon creation.Shallow copy of pnumber -> Never newly assigned -> Destructor code requests release of unallocated area in the first place, resulting in error

I wonder which of these two processes is correct.

In books, lectures, etc., it is already at some point b before shallow copying, as shown in (1).I asked you because there is no explanation about the premise that the pnumber was assigned.

Thank you.

c++ constructor

2022-09-21 17:26

1 Answers

The default copy generator will probably just copy. Even if it's a pointer.

The compiler doesn't know if the pointer should be one or two, so it just copies the address...

So b.pnumber copies a.pnumber instead of accepting assignments. That is, b in the copy generator.pnumber = a.pnumber occurs. b.new for pnumber does not occur anywhere.

If &(a.pnumber) was 0x20180419, &(b.pnumber) is also 0x20180419, which can be the same as the newint (5/code> executed by the creator of A. There is only one integer in the memory location 0x20180419, but only 0x20180419, a number that points to the memory location, is copied.

In C/C++, a shallow copy of the pointer simply means copying the address value as it is. The address is also an integer value.

(2) looks like the answer.


2022-09-21 17:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.