C ++ class copy (pointer copy)

I understand that when you make a copy of the class that defines the pointer variable, the pointer is copied, but the data that the pointer points to is not.

My question is: can we assume that the "copy of the pointer" in this case simply creates an instance of a new pointer (dynamic memory allocation) of the same type? For example, a new pointer is just a new distribution containing an arbitrary memory address, and you need to make sure that the new pointer to the corresponding memory address?

I suppose there is a fairly simple answer to this question, and I apologize for its trivial nature, but I am trying to understand pointers at a deeper level, and it came up to my research pointers on the Internet.

Hi,

Chad

+8
c ++ pointers class
source share
5 answers

The pointer will simply be copied as a value - so both classes will point to the same source memory, with no new allocation happening. An invalid copy is what the language does by default.

If you need to allocate new memory and make a copy of the data you must make yourself in the copy constructor. Deep copy - you have to do it yourself

edit: This is one of the advantages of C ++, you can decide how copying works. Perhaps a copy of an object that only reads memory access can avoid the cost of copying memory. You can also implement classes that only make a copy of the original data if a new object needs to write.

+13
source share

Firstly, the pointer in your class is static (i.e., the compiler knows that there is a pointer in this class and what size it is, so when creating an instance of the class dynamic allocation of memory is not required).

If you copy a class (and do not have a special copy constructor), then the pointer in the new class will point to the same place in memory as the pointer in the old class. To clarify:

#include <iostream> class A { public: int *p; }; int main() { A a,b; ap = new int(10); b = a; std::cout << "*(ap) = " << *(ap) << std::endl; // 10 std::cout << "*(bp) = " << *(bp) << std::endl; // 10 *(bp) = 3; std::cout << "*(ap) = " << *(ap) << std::endl; // 3 std::cout << "*(bp) = " << *(bp) << std::endl; // 3 return 0; } 

Now, if you want to allocate new memory when copying, you need to write a copy constructor and a copy destination constructor:

 #include <iostream> class A { public: int *p; A() : p(0) {} A(const A& other) { // copy constructor p = new int(*other.p); } A& operator=(const A& other) { // copy assignment constructor // protect against self assignment if (this != &other) { if (p != 0) { *p = *other.p; } else { // p is null - no memory allocated yet p = new int(*other.p); } } return *this; } ~A() { // destructor delete p; } }; int main() { A a,b; ap = new int(10); b = a; std::cout << "*(ap) = " << *(ap) << std::endl; // 10 std::cout << "*(bp) = " << *(bp) << std::endl; // 10 *(bp) = 3; std::cout << "*(ap) = " << *(ap) << std::endl; // 10 std::cout << "*(bp) = " << *(bp) << std::endl; // 3 return 0; } 

When you do this, you should also write a destructor (see the rule of three options ), since the memory allocated in the copy folder / copy destination constructors should be allocated if the class is destroyed:

+5
source share

Pointers do not create dynamic memory allocation. Pointers and distributions are completely different things.

If you copy a pointer that points to dynamically allocated memory, you have two pointers that point to the same allocated memory. Since you copied it, it already points to a block of memory. In particular, if you use the copy constructor created by the compiler, the new pointer will point to the same as the old pointer. You do not need to do anything about it if this is normal.

You have a problem when you need to free memory. Freeing it twice will usually cause cumulus damage, which is unpleasant. Without freeing it, this will lead to a memory leak, which may be acceptable in some cases. Freeing it before another pointer fires will also cause problems. For this reason, people who have several pointers to the same memory often go to the Boost project and use their shared_ptr template (which will be in the new new standard and present in most modern systems).

If you want each pointer to point to separate pieces of memory, you must set this by writing your own copy constructor and selecting a new piece and copying the necessary data into it. (You also need to write your own assignment operator for the same reasons and your own destructor so you can free up memory. There, a rule of thumb called Rule three says that if you need to write your own copy of a constructor, assignment operator or destructor, you you probably need to write all of them.)

+2
source share

The copied pointer will point to the same address. There is no new distribution.

+1
source share

Yes, pointers just contain memory addresses if you want to make a deeper copy that you need to encode yourself in the copy constructor.

If you always refer to the same data type with a pointer from the same class and must copy the data along with the objects, you can also consider making it a simple member, not a pointer.

0
source share

All Articles