简体   繁体   中英

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM