Static variables and recusive functions

I want to write a function which will be run recursively while at the same time work on a single array. However, I don't want the argument list to look messy, so I decided to go for static variable. Something like this:

1
2
3
4
5
6
7
8
9
int recursion(int n, /*some other arguments*/)
{
	static int* myarray = new int[n];
	static int count = 0;
	//do something
	//something else
	recursion(n, /*something*/);
	return count;
}


instead of

1
2
3
4
5
6
7
8
int recursion(int n, int* myarray, /*some other arguments*/)
{
	static int count = 0;
	//do something
	//something else
	recursion(n, a, /*something*/);
	return count;
}


The problem is, I want to delete the dynamically allocated array "myarray" when this function return to its original caller (to avoid memory leak) but at the same time, "count" must be the return value, how can I do that?

I tried having one of the argument be a static variable, with NULL being default value in the function prototype so I don't have to pass it the second time the function is called, but it wouldn't work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int recursion(int n, /*other arguments*/, static int* myarray = NULL);

//...

int main(void)
{
	//...
	int* myarray = new int[n];
	//Or
	//static int* myarray = new int[n];
	//both won't work anyway
	recursion(n, /*other arguments*/, myarray);
	delete []myarray;
	return 0;
}

int recursion(int n, /*other arguments*/, static int* myarray)
{
	static int count = 0;
	//do something
	recursion(n, /*other arguments*/); //no myarray this time
	return count;
}


Any advice on this? Thanks in advance.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
int recursion(int n, /*some other arguments*/)
{
	static int* myarray = new int[n];
	static int count = 0;
	//do something
	//something else
	recursion(n, /*something*/);
	if( myarray != NULL ) //the kludge
		delete[] myarray;
	return count;
}
Another option:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int recursion(int n, /*...*/)
{
    struct helper
    {
        static int recursion(int n, int * array, /*...*/)
        {
            /*...*/ helper::recursion(/*...*/); /*...*/
        }
    };

    int * array = new int[n];

    int count = helper::recursion(n, array, /*...*/);

    delete[] array;

    return count;
}

Useful link -> http://www.respower.com/~earlye/programming/19990916.001.htm
Last edited on
@Mathhead200: If I do it like that, then myarray will be deleted after the last call to the function, right? Then previous calls can not access it anymore.

@m4ster r0shi: Thanks for the tip and the link. That's one feature of classes , right?

Edit: Why need static in the inner function's declaration/definition?
Last edited on
Sephiron wrote:
@Mathhead200: If I do it like that, then myarray will be deleted after the last call to the function, right? Then previous calls can not access it anymore.
The if statement get's added to the stack equal to the number of times you recurse. This is after all the relevant code has run, but as I said it's a bit of a workaround and really could be done better.
@Mathhead200: heap corruption.
the new[] will only be executed the first time the function is ever called.
You delete[] the same pointer over and over again.

@Sephiron: ¿what are you trying to accomplish? (not how)
You could use vector instead.

@m4ster r0shi: ¿won't be the same result if you declared the wrapped function inside?
I was under the impression that static variables in functions were initialized when the program started, not on the first call of the function?
ne555 wrote:
¿won't be the same result if you declared the wrapped function inside?

You mean outside? Yes. But doing it like this avoids pollution of the global namespace.

So, @Sephiron, if the above is confusing, you can just do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int recursion_helper(int n, int * array, /*...*/)
{
    /*...*/ recursion_helper(/*...*/); /*...*/
}

int recursion(int n, /*...*/)
{
    int * array = new int[n];

    int count = recursion_helper(n, array, /*...*/);

    delete[] array;

    return count;
}
me555 wrote:
@Mathhead200: heap corruption.
if( myarray != NULL ) delete[] myarray;
It's got to be the simplest solution to the original problem. And yes, if using my above fix, you can't call the function more then once, so again it's not really the correct way.
Last edited on
L B wrote:
I was under the impression that static variables in functions were initialized when the program started, not on the first call of the function?

not this kind of static - the static that has global module scope happens in an indeterminate order at program start up, but static inside a function or method will get initialized upon the first invocation

edit: these kinds of policies for static appear to have been left-over from the early days of C
Last edited on
I was having a misconception.
¿What is the purpose of doing this? (Sadly, you can't declare the inside function as static)
1
2
3
4
void foo(){
  void bar(); 
  bar();
}
That's a definition of a function with a sub-function declared inside of it but not defined, and then called. Unless there is a definition for foo::bar(), it can't work.
1
2
3
4
5
6
7
void foo(){
  void bar(); 
  bar();
}

//void foo::bar(){} //error: 'foo' is not a class or namespace
void bar(){}
Now, ¿why would I want to do that?
Line 7 is a definition for function bar().
Line 2 is a declaration for function foo::bar()

See the difference?
Topic archived. No new replies allowed.