Are there any downsides to passing structs by value in C, rather than passing a pointer? Are there any downsides to passing structs by value in C, rather than passing a pointer? c c

Are there any downsides to passing structs by value in C, rather than passing a pointer?


For small structs (eg point, rect) passing by value is perfectly acceptable. But, apart from speed, there is one other reason why you should be careful passing/returning large structs by value: Stack space.

A lot of C programming is for embedded systems, where memory is at a premium, and stack sizes may be measured in KB or even Bytes... If you're passing or returning structs by value, copies of those structs will get placed on the stack, potentially causing the situation that this site is named after...

If I see an application that seems to have excessive stack usage, structs passed by value is one of the things I look for first.


One reason not to do this which has not been mentioned is that this can cause an issue where binary compatibility matters.

Depending on the compiler used, structures can be passed via the stack or registers depending on compiler options/implementation

See: http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html

-fpcc-struct-return

-freg-struct-return

If two compilers disagree, things can blow up. Needless to say the main reasons not to do this are illustrated are stack consumption and performance reasons.


To really answer this question, one needs to dig deep into the assembly land:

(The following example uses gcc on x86_64. Anyone is welcome to add other architectures like MSVC, ARM, etc.)

Let's have our example program:

// foo.ctypedef struct{    double x, y;} point;void give_two_doubles(double * x, double * y){    *x = 1.0;    *y = 2.0;}point give_point(){    point a = {1.0, 2.0};    return a;}int main(){    return 0;}

Compile it with full optimizations

gcc -Wall -O3 foo.c -o foo

Look at the assembly:

objdump -d foo | vim -

This is what we get:

0000000000400480 <give_two_doubles>:    400480: 48 ba 00 00 00 00 00    mov    $0x3ff0000000000000,%rdx    400487: 00 f0 3f     40048a: 48 b8 00 00 00 00 00    mov    $0x4000000000000000,%rax    400491: 00 00 40     400494: 48 89 17                mov    %rdx,(%rdi)    400497: 48 89 06                mov    %rax,(%rsi)    40049a: c3                      retq       40049b: 0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)00000000004004a0 <give_point>:    4004a0: 66 0f 28 05 28 01 00    movapd 0x128(%rip),%xmm0    4004a7: 00     4004a8: 66 0f 29 44 24 e8       movapd %xmm0,-0x18(%rsp)    4004ae: f2 0f 10 05 12 01 00    movsd  0x112(%rip),%xmm0    4004b5: 00     4004b6: f2 0f 10 4c 24 f0       movsd  -0x10(%rsp),%xmm1    4004bc: c3                      retq       4004bd: 0f 1f 00                nopl   (%rax)

Excluding the nopl pads, give_two_doubles() has 27 bytes while give_point() has 29 bytes. On the other hand, give_point() yields one fewer instruction than give_two_doubles()

What's interesting is that we notice the compiler has been able to optimize mov into the faster SSE2 variants movapd and movsd. Furthermore, give_two_doubles() actually moves data in and out from memory, which makes things slow.

Apparently much of this may not be applicable in embedded environments (which is where the playing field for C is most of the time nowdays). I'm not an assembly wizard so any comments would be welcome!