DEV Community

Discussion on: Pointers in practice

Collapse
 
jaakidup profile image
Jaaki

It depends on the use case which approach you would choose.

Passing structs by value creates a new copy as well as memory position, it is placed on the stack. Stack is cheap.

Passing by reference, the pointer points to the original struct and memory position, it is placed on the heap. Heap is expensive.

So, if your struct is large or if you want to modify the values of the original struct down the line, then you go with pointers, otherwise stick with values.

That's the simple version :D

Collapse
 
mjb2kmn profile image
MN Mark

Excellent point about the Heap vs Stack. I keep expecting pointers would be more efficient, not thinking of values being on the Stack.