static关键词及其生存周期
in Cpp with 22 comments

static关键词及其生存周期

in Cpp with 22 comments

static作用范围及其生存周期

全局普通变量及static全局变量

全局变量存放在静态区,生存周期从程序开始到程序结束,整个程序都可以访问它,其它文件通过extern关键词访问。

static全局变量放在静态区,生存周期从程序开始到程序结束,只在本文件内可以访问,其它文件无法通过extern关键词访问。

局部变量及static局部变量

普通局部变量放在栈区,作用域之外无法访问,出作用域即被弹出栈,生存周期为声明到作用域结束。

static局部变量放在静态区,程序开始的时候就已经放在静态区了,没被初始化的static变量默认为0。多次调用函数,只有第一次函数调用的初始化会进行赋值,后面的初始化都只是声明进入作用域,函数调用结束之后会出作用域,但是静态区的值一直在。
看一段试验代码:

#include<stdio.h>
#include<stdlib.h>
int a=10;
int b=20;
static void func()
{
    ++a;
    ++b;
    static int a=1;
    static int b=2;
    ++a;
    ++b;
    printf("%d %d\n",a,b);
}
void func2()
{
    ++a;
    ++b;
    static int a;
    static int b;
    ++a;
    ++b;
    printf("%d %d\n",a,b);
}

int main()
{
    func();
    func();
    func2();
    func2();
    printf("%d %d\n",a,b);
    return 0;
}

其输出为:

➜  tmp gcc -o test test.c
➜  tmp ./test
2 3
3 4
1 1
2 2
14 24

看完就秒懂

普通函数和static函数

普通函数和static函数都放在代码段,调用过程没啥区别,static函数仅存在于本文件中,不会与其它文件的函数重名冲突这样。

Responses
  1. Buy Ketotifen Pills Online cialis 20 vademecum Propecia For Hair Loss Good Sites To Buy Viagra

    Reply
    1. @StevRing

      cbd drops cbd drops cbd

      Reply
    2. @StevRing

      cbd tinctures cbd cbd gummies cbd oil online

      Reply
    3. @StevRing

      https://cbdhempoil2020.com/ cbd oil cbd cbd tinctures cbd vape

      Reply
    4. @StevRing

      cbd online cbd vape cbd cbd

      Reply
    5. @StevRing

      https://buycbdoil55.com/ cbd oil buy cbd oil cbd buy cbd oil online

      Reply
    6. @StevRing

      https://cbdhempoil2020.com/ cbd tinctures https://cbdhemp30.com/ - cbd cream cbd tinctures

      Reply
    7. @StevRing

      cbd tinctures cbd oil cbd gummies

      Reply
    8. @StevRing

      https://cbd-oil10.com/ buy hemp oil hemp cbd buy hemp oil cbd oil

      Reply
    9. @StevRing

      cbd gummies cbd cbd online cbd vape https://cbd-oil11.com/ - buy hemp

      Reply
    10. @StevRing

      cbd cream buy hemp buy hemp cbd gummies

      Reply
    11. @StevRing

      https://hempcbd10.com/ cbd online buy cbd oil cbd drops hemp cbd

      Reply
    12. @StevRing

      https://cbdoilhemp24.com/ cbd oil online buy cbd oil cbd drops cbd cream

      Reply
    13. @StevRing

      buy cbd oil https://cbdoil51.com/ - buy cbd cbd cream buy hemp oil hemp cbd

      Reply
    14. @StevRing

      cbd gummies https://cbdoilinn.com/ - buy hemp oil buy cbd buy cbd oil cbd gummies

      Reply
    15. @StevRing

      buy hemp https://hempcbd2020.com/ cbd tinctures cbd vape cbd cream

      Reply
  2. Priligy Junto Con Cialis http://cialibuy.com - Buy Cialis Keflex Foot Fungus Cialis Priligy Cream

    Reply
  3. Viagra Terapia Antipertensiva http://cialibuy.com - buy generic cialis Difference Between Proscar Propecia Buy Cialis Choisir Levitra En Ligne

    Reply
  4. Amoxicillin Sensitive To Sunlight http://cialisjh.com - Cialis Buy Diflucan Online Buy Cialis Buy Keflex Online

    Reply
  5. Viagra Dove Acquistare http://cialibuy.com - Cialis Propecia Doctor Spfld Ma Cialis Amoxicillin Trihydrate Powder

    Reply
  6. El Cialis Se Vende Sin Receta http://abuycialisb.com - Buy Cialis Viagra E Cialis Problemi Buy Cialis Online Fluxotine No Prescription

    Reply
  7. coursework marking [url=http://courseworkhelpxeg.com/]coursework sample of written work[/url] coursework project coursework sample do my coursework

    Reply