0

what I am missing in this move constructor? & Use case of same

Hi Please refer below code of mine on move constructor: https://code.sololearn.com/cqhooiokOsCi I found that there is some issue. I could see that there is no proper destructor call in case of move constructor scenario. why "Bye bye C_1" is not printed twice instead of other scenario of "Bye bye B_1" and "Bye bye A_1"? Please let me know what I am missing on this? Thanks a lot...!

16th Jun 2020, 5:05 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Respostas
+ 1
Because of a stray NULL Pointer assignment at line 71. You need to really careful with when and how to use NULL Pointers, stray NULL Pointers can wreck havoc to the entire system if not handled correctly. Besides, the job of a Move Constructor is to copy values and destroy the initial object. P. S. You might consider using C++11 string class instead of cstring as it is a much safer way to handle strings.
16th Jun 2020, 5:21 PM
Anubhav Mattoo
Anubhav Mattoo - avatar
+ 1
It is undefined behavior to try to print a null-pointer. "basic_ostream's operator<<(basic_ostream<>&, const char*) " function requires that the char* is non-null: http://eel.is/c++draft/iostream.format#ostream.inserters.character-2
17th Jun 2020, 2:01 PM
MO ELomari
MO ELomari - avatar
+ 1
comment the the line 77 and run the program again. ( also the line 71 is redundant )
17th Jun 2020, 3:57 PM
MO ELomari
MO ELomari - avatar
0
Hi, I updated header inclusion to string.h.... thank you... However, I could not correct issue of move constructor... We cannot comment out that nullptr as we are transferring ownership from old to new object... So , how to get rid of this issue ? P.S. I updated code to test move constructor with temporary object as well... it works fine if code block related to c1 is commented and only D1 related temp object is passed to vector push back... however, it again fails for move when done inside push_back.... i.e. my move constructor works with temporary object, not with move()... how to solve this issue is eating my head now
16th Jun 2020, 6:41 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Yeah but why it is not working for move call ? If I comment line 102 and 112, it works properly (even with setting it as nullptr) and move constructor works as expected... Something is done by move I guess
17th Jun 2020, 3:45 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Yup... Line 71 is redundant and commented.... However, I wanna print from line 77 as it shows destructor call... Okay as I am doing nullptr, I should not print... So, updated code to print only bye bye (without char*).... Now one thing confuses me about move constructor... Is it to avoid multiple copy and just transfer ownership.... So , with this ; let's focus on last case of vector one...that is related to D_1 and D_2.... As only two object are there, P3 and one temp, I expect only two destructor call (print bye bye for two different lines), but it prints 4 times.... If so, what's the purpose of move constructor? Am missing something or my understanding is not correct ? Thanks a lot Mohamed ELomari for making me understand.. appreciate your extended hand of support
17th Jun 2020, 4:16 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Hi community, I am still not able to get the move constructor... Why two times destructor is not called for d_1 and d_2 scenario?
5th Jul 2020, 7:20 AM
Ketan Lalcheta
Ketan Lalcheta - avatar