Question

I have a recursive function that has a parameter that is a reference to a 2D matrix of object pointers. My simple question is in what format do I pass that within the function so that it would work?

I wrote this code purely for example to pass my point along and in no way represents my intended use of recursion.

This code checks for a 0 object ID value in a diagonal line through the matrix to 255,255. Then prints 0 or -1.

typedef object* m256x256[256][256];

int x = 0;
int y = 0;

int isThereZero(m256x256 & m){
  if(m[x][y]->getID() == 0){ // Each object has an ID value
    return 0;
  }
  x++;
  y++;
  if(x==256){
    return -1;
  }
return isThereZero(/*I need to pass M byref again... what do I put here?*/);
}
int main{
  object* M[256][256];
  /* Initialization Code */
  cout << isThereZero(M);
  return EXIT_SUCCESS;
}

So that by the 256th recursion m is still the same reference to M

Specific question: How do I format this to get it to compile:

int isThereZero(m256x256 & m){
   return isThereZero(/*I need to pass M byref again... what do I put here?*/);
}
Was it helpful?

Solution 2

Since you state, your code does not represent your actual code, it is difficult to give an educated answer (imho).

To pass by-reference as you state in your question:

int isThereZero(m256x256 & m);

If you do not change the reference you can pass by const-reference:

int isThereZero(const m256x256 & m);

This allows for some optimizations.

Of course if you pass a pointer to the array by-value:

int isThereZero(object ***m);

This will more or less work like the const-reference version, since it only passes a pointer, not the whole array.

I hope this answers your question

OTHER TIPS

Rather than using global x and y, try something like so:

bool is_there_zero(const m256x256& m, int x = 0, int y = 0)
{
    if (m[x][y]->getID() == 0)
        return true;

    if (++y == 256) {
        y = 0;
        if (++x == 256) {
            return false;
        }
    }

    return is_there_zero(m, x, y);
}

So we bump the y value, and if that reaches the end of the row we reset it to zero and bump the x coordinate, and finally terminate the recursion when both are equal to 256. The original matrix is untouched.

In other words, we've turned it into a poor man's double for loop, only with added function call overhead and the risk of running out of stack...

As such, I'm not at all sure why you'd want to do it this way rather than just iterating. The only advantage I can see is that, with some "creative" use of the ternary operator you could turn this into a one-line constexpr function, which could potentially be evaluated at compile-time for a fixed m, but that hardly seems worth the bother.

EDIT: Re-reading the question, I see you only wanted to test the leading diagonal, so it's a bit simpler:

bool is_there_zero(const m256x256& m, int x = 0)
{
    if (x == 256)
        return false;

    if (m[x][x]->getID() == 0)
        return true;

    return is_there_zero(m, ++x);
}

Ah thank you all. You all contributed a little bit to helping me find my problem. As most of you probably realized. Just calling return isThereZero(m); Should have worked. This helped me find that the error was in the syntax of other areas of my program and only caused the compiler to tell me that the problem was with the passing and calling of this array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top