A static variable inside a function is only allocated once during the lifetime of the program.
So if I have a function like:
void f(int n) {
static int *a = new int[n];
}
and I first call
f(1)
and then
f(3)
how big will the array a be after the second call?
staticlocal variables will be initialized when the control flow reaches the declaration for the first time. In this case, since the first time you used1as thenparameter, you’ll be allocating size for oneint.Doing this kind of stuff is a bad idea. You should just use a local, non-static,
std::vectoror some other higher level container.