Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2008 Passing Variables - struct vs struct components - advantages/disadvantages?

I really hope this isn't one of those super basic questions.

Anyway, I've got a struct with 47 components, and I'm calling various functions that use anywhere from 3 to 10 of these components at once.

Is it better to call the function like:
foo(pParam->variable1, pParam->variable2, pParam->variable3)
or foo(pParam) and then in the function use pParam->variable1; pParam->variable2; pParam->variable3; ?

Thanks in advance!

like image 379
Russbear Avatar asked Feb 24 '23 19:02

Russbear


1 Answers

You should pass the struct by reference, that way you don't need to copy all the values:

void foo(mySturct pParam); //Here the struct will be copy constructed.. relatively large overhead

void foo(Val1 param1, Val2 param2, Val3 param3); // 3 parameters passed by value, the value will be copied.

void foo(mySturct &pParam); //Only the reference to the structure is passed. Single pointer size value is passed.

In general structures are there so you can unite data. So it doesn't make sense to take it apart and pass different parameters to functions.
It should stay as a single unit.

like image 175
Yochai Timmer Avatar answered Mar 16 '23 00:03

Yochai Timmer