title: Are there any downsides to passing structs by value in C, rather than passing a pointer?
body: <p>Are there any downsides to passing structs by value in C, rather than passing a pointer?</p>
  <p>If the struct is large, there is obviously the performance aspect of copying lots of data, but for a smaller struct, it should basically be the same as passing several values to a function.</p>
  <p>It is maybe even more interesting when used as return values. C only has single return values from functions, but you often need several. So a simple solution is to put them in a struct and return that.</p>
  <p>Are there any reasons for or against this?</p>
  <p>Since it might not be obvious to everyone what I'm talking about here, I'll give a simple example.</p>
  <p>If you're programming in C, you'll sooner or later start writing functions that look like this:</p>
  <pre><code>void examine_data(const char *ptr, size_t len)
  {
      ...
  }
  
  char *p = ...;
  size_t l = ...;
  examine_data(p, l);
  </code></pre>
  <p>This isn't a problem. The only issue is that you have to agree with your coworker in which the order the parameters should be so you use the same convention in all functions.</p>
  <p>But what happens when you want to return the same kind of information? You typically get something like this:</p>
  <pre><code>char *get_data(size_t *len);
  {
      ...
      *len = ...datalen...;
      return ...data...;
  }
  size_t len;
  char *p = get_data(&amp;len);
  </code></pre>
  <p>This works fine, but is much more problematic. A return value is a return value, except that in this implementation it isn't. There is no way to tell from the above that the function <code>get_data</code> isn't allowed to look at what <code>len</code> points to. And there is nothing that makes the compiler check that a value is actually returned through that pointer. So next month, when someone else modifies the code without understanding it properly (because he didn't read the documentation?) it gets broken without anyone noticing, or it starts crashing randomly.</p>
  <p>So, the solution I propose is the simple struct</p>
  <pre><code>struct blob { char *ptr; size_t len; }
  </code></pre>
  <p>The examples can be rewritten like this:</p>
  <pre><code>void examine_data(const struct blob data)
  {
      ... use data.tr and data.len ...
  }
  
  struct blob = { .ptr = ..., .len = ... };
  examine_data(blob);
  
  struct blob get_data(void);
  {
      ...
      return (struct blob){ .ptr = ...data..., .len = ...len... };
  }
  struct blob data = get_data();
  </code></pre>
  <p>For some reason, I think that most people would instinctively make <code>examine_data</code> take a pointer to a struct blob, but I don't see why. It still gets a pointer and an integer, it's just much  clearer that they go together. And in the <code>get_data</code> case it is impossible to mess up in the way I described before, since there is no input value for the length, and there must be a returned length.</p>