6

Consider this code:

struct s { /* ... */ };

void f(struct s x) { /* ... */) /* (1) */
/* or */
void f(const struct s *x) { /* ... */ } /* (2) */

When struct s has a decent size, in which case should we prefer the first form?

md5
  • 23,373
  • 3
  • 44
  • 93
  • I personally just try to pass a pointer to the struct when it becomes large, because it's a lot faster to just pass a pointer rather than an entire large structure. – Keith Miller Oct 16 '12 at 19:07
  • You didn't say what "decent" is. It could mean anything. Nice round numbers like 1024 bytes, are those decent? In addition, asking for preferences rather than absolutes is frowned upon on this site. – Mr Lister Oct 16 '12 at 19:45

3 Answers3

6

Are you asking which is better?

It depends what you are trying to do - the second form with a pointer will be more efficient. But if you just want to pass a value to f and not have to worry about side-effects then you might go with the first signature - as long as the struct is not too large.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • How is the second form more efficient? – Mike Oct 16 '12 at 19:12
  • @Mike: Because you are copying a pointer. If that struct gets larger in size than a pointer (almost certainly will be the case) then you are doing more work. – Ed S. Oct 16 '12 at 19:13
  • 2
    I'm also not sure what side-effects you are speaking of. Sure, the const can be cast away, but you're implementing the function, right? So, umm... don't do that or take away the const. I would prefer the second form. – Ed S. Oct 16 '12 at 19:14
4

I would suggest reading this.

When struct sis of decent size as you say, then you should avoid passing it by value, especially in recursive functions.

Passing a struct by value means that it gets copied before the function invocation. That results in slower execution and greater memory utilization. Also note that the struct is going to be allocating in the stack and in some systems the stack size is pretty limited.

I would suggest using a pointer in every case unless you need multiple copies of a structure to be modified by functions and you don't want those modifications only visible within each function's scope.

Community
  • 1
  • 1
zakkak
  • 1,861
  • 1
  • 19
  • 26
  • Wouldn't it make sense to just use a union in this case? – NerdOfCode Oct 12 '18 at 17:38
  • @NerdOfCode I don't see how a union would help in that case. Unions are essentially structs that may have multiple different interpretations and take up the space of the bigger possible interpretation. – zakkak Oct 16 '18 at 00:19
  • Because each element could only one element could exist at the same time. Thus, improving the memory utilization – NerdOfCode Oct 17 '18 at 15:41
  • I am not sure I follow. We don't know anything about the struct itself. But even if we assume that a union would be more appropriate the same reasoning holds for whether passing it by value or by reference. – zakkak Oct 18 '18 at 17:24
-2

Prefer the first form when you want f to get a copy of x instead of a const pointer to x.

japreiss
  • 11,111
  • 2
  • 40
  • 77
  • 2
    And why is that exactly? Care to provide some reasoning behind that statement? I think the OP understands that a copy is going on here, that's not the question. The object is `const`, so it can't be modified (unless you are really hell bent on doing so) and you avoid a copy of the entire struct. – Ed S. Oct 16 '12 at 19:12