Question:
In C++, what happens when a recursive function passes an array to itself?
2007-08-22 08:27:42 UTC
I know that in C++, arrays are passed by reference and not by value. So if I have a recursive function that accepts an array and an index as arguments, what happens on the recursion?

If I pass the inbound array to an array that is local to the function, will that prevent the recursive calls from operating directly on the initial array? And will a new instance of the local array be created with every recursion?

PS, the array is of maximum length 4 and type int, so I am not worried about memory consumption.
Four answers:
WiseElder
2007-08-22 08:51:17 UTC
It seems like you understand the issues, so rather than answer directly, I'll ask a couple of questions.

If the maximum length of the array is four, why don't you just unfold the loop (or whatever) that's operating on the array, and deal with four variables directly?

What is it that you are doing to the array that makes you not want to affect the original copy? Is that the "right" thing to be doing? You're porting something, but you don't have to be a slave to an implementation that was awkward/illogical/dumb, or that was dictated by limitations in the original language. Do you?
?
2016-11-13 09:05:45 UTC
No; the code's obvious rationale became into to place in writing some thing like int MinimumEntry (int a[], int n) {      if (n==a million) return a[0];      int effect = MinimumEntry(a+a million, n-a million); // word the a+a million and corrected call      if (a[0] < effect)          return a[0];      else          return effect; } whether that leaves open the question of what if n is 0 or much less? Rewriting that slightly, you should get the extra robust #contain #contain int MinimumEntry (int a[],unsigned int n) {      if (n==0)          throw std::underflow_error("MinumEntry given empty array");      else if (n==a million)          return a[0];     else          return std::min(a[0], MinimumEntry(a+a million, n-a million)); } it extremely is extra helpful, yet can nonetheless be enhanced, e.g. via making the function tail-recursive and via taking a vector or possibly a pair of iterators quite of a pointer right into a C array.
2007-08-22 08:44:29 UTC
In this situation the initial array will remain untouched, since you will be operating on a local copy. A new local copy will be created for every recursion and they will all stay in scope until your end condition is met, then they will proceed to leave scope in reverse order.



If you do not want to change the initial array but want to see its values, just pass by const reference, using the "const" keyword.
nzseries1
2007-08-22 08:44:50 UTC
You can't expect C++ to create a new copy of the array each time, C++ just isn't that sort of language. You would have to create some sort of array copy function, and use that before each recursive call.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...