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*/)
{
staticint* myarray = newint[n];
staticint 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*/)
{
staticint 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:
@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?
@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?
¿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 = newint[n];
int count = recursion_helper(n, array, /*...*/);
delete[] array;
return count;
}
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.
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
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.