title: Is it possible to std::move local stack variables?
body: <p>Please consider the following code:</p>
  
  <pre><code>struct MyStruct
  {
      int iInteger;
      string strString;
  };
  
  void MyFunc(vector&lt;MyStruct&gt;&amp; vecStructs)
  {
      MyStruct NewStruct = { 8, "Hello" };
      vecStructs.push_back(std::move(NewStruct));
  }
  
  int main()
  {
      vector&lt;MyStruct&gt; vecStructs;
      MyFunc(vecStructs);
  }
  </code></pre>
  
  <p>Why does this work?</p>
  
  <p>At the moment when MyFunc is called, the return address should be placed on the stack of the current thread. Now create the NewStruct object gets created, which should be placed on the stack as well. With std::move, I tell the compiler, that i do not plan to use the NewStruct reference anymore. He can steal the memory. (The push_back function is the one with the move semantics.)</p>
  
  <p>But when the function returns and NewStruct falls out of scope. Even if the compiler would not remove the memory, occupied by the originally existing structure from the stack, he has at least to remove the previously stored return address.</p>
  
  <p>This would lead to a fragmented stack and future allocations would overwrite the "moved" Memory.</p>
  
  <p>Can someone explain this to me, please? </p>
  
  <hr>
  
  <p>EDIT:
  First of all: Thank you very much for your answers.
  But from what i have learned, I still cannot understand, why the following does not work like I expect it to work:</p>
  
  <pre><code>struct MyStruct
  {
      int iInteger;
      string strString;
      string strString2;
  };
  
  void MyFunc(vector&lt;MyStruct&gt;&amp; vecStructs)
  {
      MyStruct oNewStruct = { 8, "Hello", "Definetly more than 16 characters" };
      vecStructs.push_back(std::move(oNewStruct));
  
      // At this point, oNewStruct.String2 should be "", because its memory was stolen.
      // But only when I explicitly create a move-constructor in the form which was
      // stated by Yakk, it is really that case.
  }
  
  void main()
  {
      vector&lt;MyStruct&gt; vecStructs;
      MyFunc(vecStructs);
  }
  </code></pre>