Answer the question
In order to leave comments, you need to log in
What happens to a variable/pointer in memory after it has been zeroed out?
Good afternoon, I'm just learning c++ , I came across such a topic as "transfer semantics" and one question appeared.
Let's say I have a class that has a pointer to a dynamic array and an int size variable that stores the size of the array.
class SomeClass {
int size;
int *element;
public:
SomeClass(int i) {
size = i;
element = new int[size];
}
SomeClass( SomeClass &&other ) {
size = other.size;
element = other.element;
other.size = 0;
other.element = nullptr;
}
};
int main() {
SomeClass a1(5);
SomeClass a2 = move(a1); //происходит вызов конструктора переноса
}
Answer the question
In order to leave comments, you need to log in
I see there are still no answers to this question, so I'll add a little gag.
Let's turn to the C++ Core Guidelines for clarification.
I advise, by the way, to study in detail from cover to cover.
https://github.com/isocpp/CppCoreGuidelines/blob/m...
As a result of the move, the source should remain in a state where it can be further used.
Our SomeClass object can be allocated both on the stack and on the heap. When we move it, we only move the interior of the object, not the object itself. After being moved, the object is still alive and should be able to be properly removed from both the stack and the heap. This is entirely the responsibility of the class developer. But it is not move semantics that deletes an object, and certainly not a move constructor. std::move functionis just a decorator that changes the type of an object from lvalue to rvalue . It does not deal with deleting or cleaning up the object.
Once moved, the object can be further used. If you follow the guideline, then after moving the object should become as if it was just constructed by the standard constructor. The object can be filled again and moved again. And so - any number of times.
And the object itself always occupies the same amount of memory defined by its class.
Also related:
https://github.com/isocpp/CppCoreGuidelines/blob/m...
https://github.com/isocpp/CppCoreGuidelines/blob/m...
You're just copying the value of the pointer.
You nullify the value of the pointer and this does not affect the selected data in any way.
Usually they say the copy constructor, but this is so, by the way.
1. The answer to your question. The size variable will take on the value of other.size, *element will take on the value of *other.element (as a pointer). Moreover, after calling the constructor, the other object will change, because it is passed to the address.
2. This is a wrong implementation - you are copying. The other object must not be modified. And for the pointer a2, you need to copy not the values of the pointer, but the values that are stored at this pointer. Otherwise, you will end up with members of two classes referring to the same memory area and can change it. Those. if values change at pointer a1.element, they will change in a2 as well. When copying, it is necessary to first release all the *element memory, then re-allocate it to the desired size, and only then copy the values \u200b\u200bto this area.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question