A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

CyberShadow

  • one year ago

Question about move constructors in C++

  • This Question is Closed
  1. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    This is how we define a normal move constructor: string(string&& move) // string&& is an rvalue reference to a string { data = move.data; move.data = nullptr; //What's the need of this?? } My question is , why do we need to call "move.data=nullptr;"? Why do we need to leave it in a safe state if it's a rvalue and never going to be used again?Why can't we just leave it as it is?

  2. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Well since it's a constructor, I believe you probably won't be popping it off the stack? That being said, even if you're never using it again, you will get memory leaks in the code when it's not set to null. memory leaks can be very bad if not dealt with. They can cause segmentation faults often, and leave security bugs in your code.

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    the reason you set the other to null is so you don't end up with two separate mutable strings using same underlying data, as otherwise you end up risking corruption

  4. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yeah i think i got it.. The main point of a move constructor is to steal the data from the owner, while without the nullptr call , its just sharing the data instead of stealing..... also it can lead to memory leaks.

  5. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    eh, in practice It's hardly going to lead to a memory leak -- it's a lot more likely the pointer would get freed when the original object falls out of scope or is otherwise destroyed, and you'd end up with invalid state when your string suddenly starts accessing reclaimed garbage memory

  6. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You are right @oldrin.bataku , I tested the above code without setting "move.data = nullptr" and move object's destructor gets called when it went out of scope and then we are left with dangling pointer data.

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.