3 ms·
> We can already return structs AFAIK, no? You can return a pointer to a struct, and you can pass whole structs as arguments, but not, IIRC, return them from
by teddyh 7y ago
> We can already return structs
AFAIK, no? You can return a pointer to a struct, and you can pass whole structs as arguments, but not, IIRC, return them from functions.
EDIT: Apparently you can, sort of, but not portably; how exactly it is defined to work depends on the compiler, and each compiler might define it differently. This means that if you’re using a library which returns a struct and your program use a different C compiler than the library used when it was compiled, your program will not work. I.e. there is no one defined stable ABI for functions returning structs.
Therefore I think it’s reasonable to regard it as impossible in practice.
- juped 7y agoStructs are values and you can return them like any value (or use them as parameters). I'm not sure what you mean about compilers.
- teddyh 7y agoSee, for instance, here: https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Incompatibilities.html#index-structures https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Incompatibiliti... https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Code-Gen-Options.html#index-fpcc-struct-return https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Code-Gen-Option... https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Warning-Options.html#index-Waggregate-return https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Warning-Options...
- juped 7y agoOh, I get it. This is mostly not a practically relevant issue. (Nor are pre-K&R compilers relevant, although something like this could arise among modern compilers.) As far as oddball situations go, it's far from the thorniest to deal with - it doesn't even involve C++.
- JdeBP 7y agoXe is conflating compilers and calling conventions a bit. The way that structure types are returned varies by calling convention, as indeed do a lot of other things. Mismatched calling conventions leads to problems. But structure type return values are well specified for most calling conventions, and quite a number of compilers support explicitly specifying the calling convention for mixed-language or mixed-compiler situations. * http://jdebp.uk./FGA/function-calling-conventions.html http://jdebp.uk./FGA/function-calling-conventions.html
- teddyh 7y agoMany calling conventions apparently use a method for returning structs which is inherently non-thread-safe. Also from that link: > 32-bit cdecl calling convention > For return values of structure or class type, there is wide incompatibility amongst compilers. Some make the return thread-safe, by breaking compatibility with the 16-bit cdecl calling convention. Some retain compatibility, at the expense of their 32-bit cdecl calling convention not being thread-safe. The ones that break compatibility don't all agree with one another on how to do so.