4 ms·
Just an interesting aside: returning structs directly (e.g. `struct foo bar();`) was added in V7 UNIX (1979). That said, the convention for it was pretty archai
by chjj 4y ago
Just an interesting aside: returning structs directly (e.g. `struct foo bar();`) was added in V7 UNIX (1979). That said, the convention for it was pretty archaic: behind the scenes PCC used a static return buffer and the caller knew to copy the struct from the returned pointer afterwards. So what looked like thread-safe code was actually totally broken by today's standards.
GCC still supports this with -fpcc-struct-return[1] (though, the modern man page doesn't seem to mention the static return buffer).
Also just because there were no threads back in the day doesn't mean static return buffers were okay. In some cases, invoked signal handlers could still call something and corrupt your statically allocated return buffer. So making any system call after receiving your static return pointer was a footgun to watch out for:
struct foo *bar = some_lib_func();
time(0); /* potential breakage */
[1] https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Incompatibilities.html https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Incompatibilities...
- mikebenfield 4y agoThat’s really surprising to me to read; surely figuring out a better convention for returning a struct would never have been a challenge, right?
- fweimer 4y agoI think the GCC switch only changes the threshold when registers aren't used. The address of the temporary struct that the called function writes to is always passed as a hidden argument. The compiler will allocate the memory on the stack, so there's no impact on thread safety.