开发者

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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜