4 ms·
It returns a copy of it. Example: #include <stdio.h> struct foo { char space[1024]; }; struct foo f() { struct foo f; printf("address of f is %p\n
by tosseraccount 14y ago
It returns a copy of it.
Example:
#include <stdio.h>
struct foo { char space[1024]; };
struct foo f()
{
struct foo f;
printf("address of f is %p\n",&f);
return f;
}
int main()
{
struct foo g;
g = f();
printf("address of g is %p\n",&g);
return 0;
}
produces this output:
address of f is 0x7fbfffec30
address of g is 0x7fbffff050
- Jare 14y agoIf I remember correctly (been a long time), most compilers would implement many cases of struct return by letting the caller pass the address of a block of (stack) memory to contain the return value. The function can then optimize things to get rid of its local copy, operate directly on the intended target, and the caller doesn't need to perform any copies.
- VMG 14y agoThanks, I didn't know that.