3

What is recommended to use, passing a structure as a pointer to const i.e.


int doCalculations(const MyStruct* my_struct);

or passing the struct by value, as in,


int doCalculations(MyStruct my_struct);

and why?

In C++ I recall reading somewhere that passing references to const should be used when the struct/Class has a non-trivial constructor. In C although there are no constructors, I imagine it would still take some time to make a local copy of the struct.

Grieverheart
  • 510
  • 5
  • 16
  • It depends on how your function is going to use the `struct`. If it has to change something inside the `struct` you need a pointer. Otherwise passing the `struct` itself is preferable. – Kninnug Jul 08 '13 at 15:55
  • 4
    @Kninnug passing by value uses unnecessary amounts of stack. More common guidance would be that you should pass by pointer to const - `const MyStruct*` - when `sizeof(MyStruct) > x` for some smallish value of `x` – simonc Jul 08 '13 at 15:59
  • 2
    @simonc: I guess you meant 'pointer to const', not 'const pointer'... – a1ex07 Jul 08 '13 at 16:01
  • @a1ex07 Yes, thanks, I meant `const MyStruct*`. I've corrected my comment now – simonc Jul 08 '13 at 16:03
  • Recommendation will largely depend upon the size of the struct. – 0decimal0 Jul 08 '13 at 16:25

2 Answers2

4

If you intend to modify your struct then you obviously have to pass it by pointer.

If you do not intend to modify your struct then it is still general practice to pass by pointer to avoid unnecessary copying. A pointer will take up sizeof(void *) memory, but may be passed by register (thus not using stack memory at all).

Passing a struct by value will almost always make a copy of it on the stack.

Two scenarios where you have to choose:

  • If your struct may be modified by an external force (i.e. it has volatile) fields then you will probably want to pass it by pointer in order to always have the most up to date version.
  • If your struct may cease to exist during execution of your function (e.g. another thread frees that memory) then you will obviously have to make a copy of it (pass by value).
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • *If you intend to modify your struct then you obviously have to pass it by pointer.* and *If you do not intend to modify your struct then it is general practice to pass by pointer to avoid unnecessary copying.* I don't understand the difference between these two sentences. – 0decimal0 Jul 08 '13 at 16:28
  • 1
    @PHIfounder general practice != requirement. But i have clarified it a bit. – Sergey L. Jul 08 '13 at 16:31
  • @SergeyL. Is your last point regarding struct ceasing to exist purely theoretical? – Pacerier Sep 22 '13 at 17:53
  • @Pacerier No, but those cases are generally intentional and programmer controlled. Example: producer-consumer threading on a stack. One you "consume" an element another thread will soon overwrite that memory, so you are better off with copying. – Sergey L. Sep 23 '13 at 13:07
0

There are two reasons why passing a structure by value is not advised:

1). The amount of time taken to copy the structure contents into a new temporary variable.

2). The amount of stack space that would be wasted, especially if the structure is large.

Hence using a pointer to const is a much more viable option.

Nithin Bhaskar
  • 686
  • 1
  • 5
  • 9