In olden days of C

From "The C Programming Language" 1st eddition by Brian Kernighan and Dennis Ritchie, section 6.2 Structures and Functions:

There are a number of restrictions on C structures. The essential rules are that the only operations that you can perform on a structure are take its address with &, and access one of its members. This implies that structures may not be assigned to or copied as a unit, and that they can not be passed to or returned from functions. (These restrictions will be removed in forthcoming versions.) Pointers to structures do not suffer these limitations, however, so structures and functions do work together comfortably. Finally, automatic structures, like automatic arrays, cannot be initialized; only external or static structures can.

The restrictions on structures were indeed removed in the 1980s and structures as first class objects were in widespread use by the time these features were codified in the original C language standard published in 1989.

Originally I thought that the aggregate-return warning was to alert on code that would not be portable to older (pre-ANSI) compilers. But I think I was wrong since the warning is only triggered by returning structs from functions, not any of the other originally restected uses e.g. assignment, passing as arguments, or initialization of automatic objects. Therefore it can't have been much use for this purpose.

Opinions on the Internet

When researching the purpose of this warning I came accross various Internet posts expressing concerns about returning structs suggesting it was "considered poor style to return an aggregate in C; passing an out pointer is preferred...".

Nothing could be further from the truth. I think it's the existence of this warning in GCC that makes people think there must be some rational objection to the practice.

Not only is returning a value from a function much clearer (since that is the only way to express a true "out" value in C) it is not inefficient and possibly more effecient (on x86-64) than the "out" (non-const) pointer. This is because if a return value is small enough it will be returned in machine registers.

Returning structs by value (of any size) does not allocate extra space on the stack.

Actual legitimate use?

I have heard rumors that there exist some platforms or ABI specifications that cannot support returning objects of aggregate type by value. I don't know which platforms or ABIs might have had this limitation, or if any of them are still in use, but I think this might have been the reason for the warning originally.

Clang doesn't do it

The Clang compiler accepts the command line option for this warning, but never actually issues any warning messages.

Let's look at the code

An example for a struct that doesn't fit into registers.

This example shows one additional instruction for the value return case, but it's a register to register move, a very fast instruction that doesn't access memory (or cache) at all. The slower instructions, the actual memory accesses, are the same.

This is an example that does fit into registers.

This example shows that when the struct fits into registers, no memory access is required at all.

Valid HTML 5