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.

Leave a Comment