title: Is the following code returning a reference to a stack object or is it valid?
body: <p>I have this code which calls a function in the current process from a DLL that returns the value by reference. One person I talk to says it is correct and another states that it is returning a reference to a stack object and that it is undefined behaviour. I am unsure which person is more factual as I don't have as much experience as either of them.</p>
  
  <pre><code>// Function call inside of the current process at a specific address. 
  // Returns a matrix from the given meshcomponent and an id
  FMatrix* GetMatrix(MeshComponent* mesh, FMatrix* result, int id) {
      return reinterpret_cast&lt;FMatrix*(__fastcall*)(MeshComponent*, FMatrix*, int)&gt;(address)(mesh, result, id);
  }
  
  // Gets the wanted 3D Vector out of the Matrix
  void GetLocation(MeshComponent* mesh, FVector* result, int id) {
      FMatrix matrix;
      GetMatrix(mesh, &amp;matrix, id);  &lt;-- // When the function ends, is it possible for other stack operations to overwrite the matrix? Or is this valid?
      *result = static_cast&lt;FVector&gt;(matrix.WPlane);
  }
  
  // Usage, running from a DLL inside the process:
  ...
  FVector location;
  GetLocation(mesh, &amp;location, id);
  ...
  </code></pre>
  
  <p>The code as it is does work, but if it is truly returning a reference to a stack object, I know that could break at any point and I will change it accordingly. </p>