微信小程序本地存储与登录页面处理实例详细讲解
560
2022-08-27
ANSI C (4) —— 存储类别和类型限定
register
寄存器(register)变量的被访问速率远远高于内存的被访问速率,所以编译优化常常这样做:将循环控制变量和使用频繁的变量安排在CPU的寄存器中。
取地址符&不能用于寄存器变量。 比如下面的代码将会发生错误: error: address of register variable ‘i’ requested
int i; for(i=0;i<10000;i++){ if(i == 100){ printf("this is 100.\n"); printf("%p\n",&i); } }
auto
auto修饰的变量在离开自身的作用域后其占用的内存自动释放。事实上,大多数情况下我们用的变量都是auto类型的。
auto变量必须定义在函数体内
int sum(int a,int b){ auto int s=a+b; //仅仅在函数内可见 return s;}auto int a=12; //error: file-scope declaration of 'a' specifies 'auto'
extern
extern的用法 —— C++说明
#include head.cpp int get(){ return 23;}int a[5] = {1,2,3,4,5}; extern.cpp和head.cpp在同一文件夹中,这份代码是可以正常运行的。 当然,我们也可以将extern.cpp的中引用放在头文件中,直接”#include”即可 head.h #ifndef HEAD_H_#defineextern int get();extern int a[5];#endif extern.cpp: #include C++和C的混编 #include temp.cpp #include 编译执行: [edemon@CentOS workspace]$ gcc -c chello.c -o chello[edemon@CentOS workspace]$ g++ -c temp.cpp -o temp[edemon@CentOS workspace]$ gcc temp chello -lstdc++ -o exe[edemon@CentOS workspace]$ ./exehello! C调用C++ 的函数 在C++ 中创建可被C调用的函数,仍然需要extern “C”。不过函数体可用C++完成。 例子: ctemp.c #include cpphello.cpp #include 编译执行 [edemon@CentOS workspace]$ gcc -c ctemp.c -o ctemp[edemon@CentOS workspace]$ g++ -c cpphello.cpp -o cpphello[edemon@CentOS workspace]$ gcc cpphello ctemp -lstdc++ -o temp[edemon@CentOS workspace]$ ./temphello! volatile volatile告诉编译器,变量的值可能被改变,不要做编译优化。 比如下面的代码 volatile int p = 23;cout< volatile 告诉编译器p是随时可能发生变化的,每次使用它的时候必须从p的地址中读取,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。上面的代码就有可能出现这样的情况:cout< int square(volatile int *ptr) { return *ptr * *ptr; } 事实上我们期望它返回这样的数值: int square(volatile int *ptr) { int p = *ptr;return 但是编译器给我们返回了这样的数值: int square(volatile int *ptr) { int p1 = *ptr;int p2 = *ptr;return static static全局变量 static全局变量即静态全局变量,它只在定义它的源文件内有效,其他源文件不能访问。存储于全局存储区(初始化的或未初始化的)。 static局部变量 static局部变量同样存储在全局存储区(初始化)。函数调用静态局部变量的时候修改变量后离开,下次读的时候从全局存储区读出的静态局部变量就是上次修改后的值。 程序执行前,就分配存储空间,如果定义时没有初始化,那么系统赋予0,且仅仅初始化一次。 C++ primer中也提到过“静态局部变量保存在全局数据区,而不是保存在栈中” 他的存储空间和内容是不会因函数的结束而消失,但是他仅仅在函数体内是可见的。 int count(){ static int s=0; return s++;}int main(){ int i; for(i=0;i<7;i++){ printf("%d ",count()); } return 0;}/*0 1 2 3 4 5 6*/ const 被const修饰的变量是只读的,不能被人为修改,即已定义的const变量不能作为左值。一个const变量必须在定义的时候就被初始化。 const和指针的关系: 按照const在data type *的前后分为: const修饰的是数据类型,则为常量指针。(const char* p) const char *p = "hello world"; *p = 'p'; //error: assignment of read-only location ‘*p’ p = "p"; // it's ok. const修饰的是指针本身,那么则为指针常量。(char* const p) char * const p = "hello world";*p = 'p'; //it's okp = "p"; // error: assignment of read-only variable ‘p’ 由此,我们得到启发,可以通过上面的方法,改变 const data *pointer或者data * const pointer ANSI C说,const修饰的变量是不能作为数组的长度,结果我本地出现了神奇的事情: temp2.c: #include man gcc: ansi In C mode, this is equivalent to -std=c89. In C++ mode, it is equivalent to -std=c++98. [edemon@CentOS workspace]$ gcc -std=c89 -o temp2 temp2.c[edemon@CentOS workspace]$ ./tmep2 这是怎么啦? -_-|| 另外,const变量不能作为switch,case后的常量。 #include 编译: [edemon@CentOS workspace]$ gcc temp3.ctemp3.c: In function ‘main’:temp3.c:8:3: error: case label does not reduce to an integer constant case 但是在c++中这是可以的。 #include 编译 [edemon@CentOS workspace]$ g++ temp3.cpp 这是因为C中的const不是真正的静态,而c++中const数值则是常量。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~