+ 4

Can you plz help me with std::move? Thank you already :)

Take a look at the code I tagged below, It demonstrates move semantics in C++. std::move function, as far as I know, changes the ownership of the data. But here, the string is in stack, and the vector is in heap, then how the compiler makes it work without copying? The newly added string has to be in the adjacent memory address, but the string to be added is somewhere else. Then how does the move semantics work better than copy semantics here? I mean, it has to copy the string in the adjacent memory right? How can it just "move"? https://code.sololearn.com/cPg7xtWloa1i/?ref=app

11th Aug 2021, 2:22 AM
Rishi
Rishi - avatar
2 Antworten
+ 2
If no exernal data (e.g. heap memory) is involved, than Move is indeed just Copy (except for it changing the original) But, while the string variable with all its pointers is in the stack, your string value may actually be stored in the heap (expecially if it's a long string) So just let the compiler and the std libraries do their job, don't worry too much about it and use move semantics whenever you think you should
11th Aug 2021, 3:04 AM
Angelo
Angelo - avatar
+ 1
Angelo tnx, have a nice day :)
11th Aug 2021, 10:05 AM
Rishi
Rishi - avatar