How can I free built-in data token in static area
How can I free built-in data token in static area over using static keyword after a some time pass or when some condition meet ?
ex;
int func2 ( void ) {
static int i = 0 ;
// some work ;
if ( i == 20 ) {
return i ;
// some thing to give static memory place to memory
}
}
int main ( void ) {
开发者_如何转开发 //under some condition, call func2 iteratively
// when return value is 20, then break the iteration
}
You can't (unless i is an int* and you allocate and delete it manually) but : when you do that static i = 0;
at first call you create and initialize i
.
When func2 is call second time, this line will be ignore :i
already exist.
int func2(void) {
static int i = 0;
std::cout << "i " << i << std::endl;
i += 20;
return i;
}
int main() {
func2();
func2();
return 0;
}
will out :
i 0
i 20
but :
int func2(void) {
static int i = 0;
i = 0; // look
std::cout << "i " << i << std::endl;
i += 20;
return i;
}
int main() {
func2();
func2();
return 0;
}
will out :
i 0
i 0
But do you really need a static one here ?
精彩评论