Arne Rabaey
Arne Rabaey

Reputation: 31

c++ pass on parameter by reference to another function in a function

I am fairly new to c/c++ and I am trying to build a program for a genetic algorithm (using MS Visual Studio 2013). I will spare you the details of this program, but I do have a problem with 'passing parameters by reference'. Can I pass on a parameter by reference to another function, inside a function? Hereunder you can find a simple example of my code.

struct solution{
int list1[100];
int list2[100];
int list3[100];
int list4[100];
};

void function1(solution& foo)
{
// Algorithm that fills list2
function2(foo);   // Fills in list3
function3(foo);   // Fills in list4
}

void function2(solution& foo)
{
// algorithm to fill in list3
}

void function3(solution& foo)
{
// algorithm to fill in list4
}

void localSearch(solution& foo)
{
for(int i = 0; i < 10; i++)
{
    // Change a random value in list1 of foo
    // Rerun function1 and see if it is a better solution
    function1(foo);
}
}

int main()
{
solution bar;
// Fill list1 of bar randomly by other function

function1(bar);
// Output finished solution

return 0;
}

If I try to do this, I get all sorts of errors... Next to that, my solution struct gets corrupted and the first position in list1 randomly changes back to 0. I tried several things to mitigate this, but nothing seems to work. If I just pass on the solution to function1 by value, the programs seems to run, but more slowly, because it has to copy this large struct.

So my questions are:

1) Is it possible to pass (by reference) on a parameter that was passed by reference to another function, in function1?

2) What would be a better solution?

Upvotes: 1

Views: 6278

Answers (1)

Thomas Matthews
Thomas Matthews

Reputation: 57749

1) Is it possible to pass (by reference) on a parameter that was passed by reference to another function, in function1?

Yes, it is possible to pass the same variable, by reference to another function.

void f1(My_Class& m); // Forward declaration.
void f2(My_Class& m);

void f1(My_Class& m) // Definition.
{
    f2(m);
}

void f2(My_Class& m)
{
   ;
}

The forward declaration gives the compiler a "heads up" on how functions are to be used (their syntax). Without forward declarations, the compiler would get the knowledge from their definitions. However, the definitions or forward declarations must come before the function call.

2) What would be a better solution?

Here are some ideas to improve your solution:
1) Use std::vector instead of arrays.

2) Consider a std::vector of structures, rather than an a structure of arrays:

   struct List_Record
   {
      int item1;
      int item2;
      int item3;
      int item4;
   };
   std::vector<List_Record> My_Lists(100);
// or
   List_Record My_Array[100];

Having the items in a structure or record allows better data cache management by the processor (all items in a row are placed contiguously).

3) Create a method in the structure for initialization.
You should have a constructor that loads the data items with a default value.
Consider adding a method that loads the data items from a file (very useful for testing).

Upvotes: 2

Related Questions