title: Is it safe to return local struct variable's member variable?
body: <pre><code>struct SGetWindow
  {
      HWND__* pWindow;
  };
  </code></pre>
  
  <pre><code>SGetWindow getWindow;
  EnumWindows(GetWindowProc, reinterpret_cast&lt;LParam&gt;(&amp;getWindow));
  return getWindow.pWindow;
  </code></pre>
  
  <p>Using SGetWindow struct as LParam, HWND__* pWindow is successfully updated.</p>
  
  <p>I wonder if this usage of returning member variable of local struct variable is safe.</p>
  
  <pre><code>return getwindow.pWindow;
  </code></pre>
  
  <p>Is it safe? or cause undefined behavior?</p>
  
  <p><strong>EDIT:</strong> I added an image for understanding.</p>
  
  <p><a href="https://i.stack.imgur.com/DMGV3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DMGV3.png" alt="Q1"></a></p>
  
  <p>I understood that the value of pointer (0x00000000000506D8 or 0x00000000000606D8) 1) is valid after function termination, 2) will be copied. But I don't think that guarantees safeness because the matter it seems is 'access (local struct member variable that no longer exist), and return its value'.</p>
  
  <p><strong>EDIT2:</strong></p>
  
  <p><strong>I was thinking wrong and I was so dumb.</strong></p>
  
  <p><strong>According to what I was thinking, even returning local int variable would lead to undefined behavior.</strong> For example,</p>
  
  <pre><code>int func()
  {
  int a = 10;
  return a;
  }
  </code></pre>
  
  <p>Local variable 'a' no longer valid when function exits. Thus, return value is undefined behavior.</p>
  
  <p><strong>However, this isn't. Because it is 'return by value' type. The value is copied (somewhere), and the copy process is before local variable goes out of scope.</strong></p>
  
  <p>Operation sequence would be something like</p>
  
  <ol>
  <li>Allocate n bytes for local variable and set its value as 10.</li>
  </ol>
  
  <p><strong>2. Copy the local variable's value to 'somewhere'.</strong></p>
  
  <ol start="3">
  <li>Function exits and local variables become invalid (reverting EBP).</li>
  </ol>
  
  <p>"Another advantage of return by value is that you can return variables (or expressions) that involve <strong>local variables declared within the function without having to worry about scoping issues. Because the variables are evaluated before the function returns, and a copy of the value is returned to the caller, there are no problems when the function’s variable goes out of scope at the end of the function.</strong>"</p>
  
  <p><a href="https://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/" rel="nofollow noreferrer">https://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/</a></p>
  
  <p>"<strong>The copy-initialization of the result of the function call is sequenced-before the destruction of all temporaries at the end of expression</strong>, which, in turn, is sequenced-before the destruction of local variables of the block enclosing the return statement."</p>
  
  <p><a href="https://en.cppreference.com/w/cpp/language/return" rel="nofollow noreferrer">https://en.cppreference.com/w/cpp/language/return</a></p>
  
  <p>EBP revert</p>
  
  <p><a href="https://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames" rel="nofollow noreferrer">https://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames</a></p>
  
  <p>Shame on me!
  Thank you for kind answers by the way.</p>