C语言中的静态局部变量

时间:2024-11-03 14:33:32

代码:

 0x601070 0x7ffcf44243fc
0x60106c 0x60106c 0x60106c [hu@localhost test]$ cat test.cpp
#include <iostream>
#include <string>
#include <cstdio> using namespace std; void func(){
static int a = ;
cout<<&a<<endl;
cout<<a<<endl;
a++;
} int main(){
static int a = ;
int b = ;
cout<<&a<<" "<<&b<<endl;
func();
func();
func(); return ;
}

输出:

0x601070 0x7ffcf44243fc
0x60106c
1
0x60106c
2
0x60106c
3

说明:

静态局部变量存储在全局区,全局变量、静态局部变量、静态全局变量都在静态存储区分配空间。