DEV Community

Discussion on: Pointers in practice

jaakidup profile image

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

mjb2kmn profile image
MN Mark Author

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