is my implementation proper? Why also move constructor not called | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

is my implementation proper? Why also move constructor not called

Hi all I would like to know whether memory is managed properly or not for all functions or not? Also why obj_4 is not calling move constructor? https://code.sololearn.com/cA8A750A17a4

12th Feb 2021, 5:39 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 2
Yes, everything should be alright now. Moving is not equal to destroying an object. While it is often used on rvalues that are about to be destroyed, that does not always have to be the case, such as when you forcibly move "obj4". The object will still have its original scope-based lifetime and you can operate on it, which is why moved objects should be left in a valid state. The reason the destructor of "obj4" does not print the member value is that the move assignment set the field to a nullptr, so there is nothing to print (you "stole" the data from it, after all). This is important because if "obj4" still pointed to the allocated memory and would be destroyed earlier than "obj5", the memory would be deallocated, and "obj5" would from there on refer to an undefined memory region.
14th Feb 2021, 8:55 AM
Shadow
Shadow - avatar
+ 2
Memory management looks good on first glance, except that the assignment operator calls delete instead of delete[] and you might want to guard against self-assignment. The move constructor is not called because it is optimized away. "When the initializer is a prvalue, the move constructor call is often optimized out (until C++17)/ never made (since C++17), see copy ellision." From: https://en.cppreference.com/w/cpp/language/move_constructor See: https://en.cppreference.com/w/cpp/language/copy_elision If you really want to make sure to call the move constructor, wrap the temporary in a call to std::move(). However, right now, your move constructor is really just a copy constructor. That is fine, but goes a bit against the idea of "stealing" the resources. I think what you'd want to do is to take ownership of the string by copying the pointer itself, not what it points to, and then set the other pointer to a nullptr to prevent accidental destruction of the data. That's at least what would be my idea.
12th Feb 2021, 6:35 PM
Shadow
Shadow - avatar
+ 1
Shadow Thanks for a big catch of move and copy constructor both being almost same... Updated delete[] , self assignment and added code of move assignment. Now, destructor getting called twice and it looks good to me as one is for obj_4 and other for obj_5.. However, why member value is not printed from destructor during second call? Plus is destructor call okay? shouldn't it call destructor , then setmethod and then second destructor as we already moved object by calling move?
14th Feb 2021, 5:34 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks for your help
14th Feb 2021, 12:03 PM
Ketan Lalcheta
Ketan Lalcheta - avatar