Passing data through queues by reference
Since the data-type of a queue is arbitrary, we also have the ability to pass data by reference instead of by value. This works in a way that is similar to passing arguments by reference to a function.
The next example-program is in mainQueueCompositePassByReference.c
. It is very similar to the prior example-program. However, instead of using queue items which are structs
, it uses queue items that are pointers to structs
.
When to pass by reference
A queue will make a copy of whatever data it is holding. So, if the data-structure being queued is large, it can be inefficient to pass the data-structure by value:
- Sending and receiving from queues forces a copy of the queue item each time.
- The resulting queue can get very large if the queue items are large data structures.
When large data structures need to be queued, passing the queue items by reference can be a good idea. Here’s an example of a larger...