Reputation: 121
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.
// 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<FMatrix*(__fastcall*)(MeshComponent*, FMatrix*, int)>(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, &matrix, id); <-- // When the function ends, is it possible for other stack operations to overwrite the matrix? Or is this valid?
*result = static_cast<FVector>(matrix.WPlane);
}
// Usage, running from a DLL inside the process:
...
FVector location;
GetLocation(mesh, &location, id);
...
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.
Upvotes: 0
Views: 74
Reputation: 19093
According to your comment
FVector is just a struct of a 3D Vector containing {x, y, z}
this line copies data to a location pointed by result
*result = static_cast<FVector>(matrix.WPlane);
There is no returning by reference. Example for this would be
int& foo() {
static int i;
return i;
}
Upvotes: 1