I was wondering Are these scenarios ever possible for a compiler to do?
first we define SomeDataType as:
struct SomeDataType{
public:
int a;
int b;
int c;
};
Scenario #1_regarding a callee function having reference parameter like this:
void doSomething(SomeDataType & input){
...
}
assuming the function isn't inline and only variables in scopes of caller functions are passed to this function in a program and regarding the fact that references aren't necessarily pointers, the part of memory where the input argument is placed is shared between stack frame of any caller function and stack frame of "doSomething" callee function so that "doSomething" can address that parameter just the way it addresses any local variable in its local scope which is by adding offset to a base pointer that determines beginning address of its stack frame.
Scenario #2_This seems more improbable to me but anyway; regarding a callee function that returns a structure of type "SomeDataType":
SomeDataType doSomething(){
SomeDataType someStruct;
...
return someStruct;
};
the part of memory where the structure "someStruct" resides is shared between stack frame of any caller and stack frame of "doSomething" callee function so considering the following statement in a caller function:
SomeDataType TheStruct=doSomething();
in the scope of that caller using "TheStruct" results in using the same part of memory where "SomeStruct" in the scope of callee resides meaning basically the callee function doesn't copy "someStruct" anywhere and even if copying was necessary like when there was a statement like below in a caller function indicating that destination isn't a structure in the caller scope:
*pntrToSomewhere=doSomething();
it'll be the caller duty to copy contents of that shared part to where that pointer tells.