C中的存储类、链接和内存管理
来源:互联网 发布:如何用sql语句创建表 编辑:程序博客网 时间:2024/05/16 01:15
学习了一阵子STM32之后深感C的很多知识还仅仅局限于考试,于是重新翻开了C primer plus。
一、存储类
1、自动变量
/* hiding.c -- variables in blocks */#include <stdio.h>int main(){ int x = 30; /* original x */ printf("x in outer block: %d\n", x); { int x = 77; /* new x, hides first x */ printf("x in inner block: %d\n", x); } printf("x in outer block: %d\n", x); while (x++ < 33) /* original x */ { int x = 100; /* new x, hides first x */ x++; printf("x in while loop: %d\n", x); } printf("x in outer block: %d\n", x); return 0;}
这是C primer上的一段代码,也比较好理解,外部的x值被内部的覆盖了,但是在while循环条件中使用的仍然是代码块外部的x。
2、寄存器变量
通常,变量存储在计算机内存中,而寄存器变量(register)可以被存储在CPU存储器中或是速度最快的可用内存中,但是无法获取register变量的地址。
eg. register int quick;
3、静态变量(static variable)
静态变量指的是变量的位置固定不变。
具有文件作用域的变量必须为static
eg.
/* loc_stat.c -- using a local static variable */#include <stdio.h>void trystat(void);int main(void){ int count; for (count = 1; count <= 3; count++) { printf("Here comes iteration %d:\n", count); trystat(); } return 0;}void trystat(void){ int fade = 1; static int stay = 1; printf("fade = %d and stay = %d\n", fade++, stay++);}输出的fade为:1 1 1
而stay为:1 2 3
fade在每次调用trystat时都初始化,而stay只初始化一次。
所以,具有静态存储时期为:从一次函数调用到下一次调用,计算机都记录着他们的值。
4、具有外部链接的静态变量(external storage class)
/* global.c -- uses an external variable */#include <stdio.h>int units = 0; /* an external variable */void critic(void);int main(void){ extern int units; /* an optional redeclaration */ printf("How many pounds to a firkin of butter?\n"); scanf("%d", &units); while ( units != 56) critic(); printf("You must have looked it up!\n"); return 0;}void critic(void){ /* optional redeclaration omitted */ printf("No luck, chummy. Try again.\n"); scanf("%d", &units);}
其实main()中的units定义可以省略
当在使用多文件构成程序时,内部链接和外部链接才显得格外重要
二、存储类说明符
在C中有5中存储类说明符:auto,register,extern,static和typedef
三、ANSI C的类型限定词
1、const
!初始化必须赋值;
const int *pp指向的值不可以改变
int *const PP总是指向同一个地址
int const *PP指针不可以改变所指向的值
2、volatile
该变量除了可被程序改变外还可以被其他代理改变,典型的,它被用于硬件地址与其他并行运行的程序共享的数据,例如,一个地址种可能保存着当前的时钟时间,不管程序做什么,该地址的值都会随着时间改变,另一种情况是改地址用来接收其他计算机的信息。
一个值可以同时是const和volatile。例如,硬件时间一般设定为不能由程序改变,但是他被程序以外的代理改变,这是他同时成为const,volatile
eg. const volatile intloc
3、restrict
//
四、结构和其他数据形式
当定义一个纸箱结构的指针时,可以将它指向任意的及结构,但是必须要使用&,结构和数组不一样,结构名不是他的地址
指针引用的方法: him->income = fellow[0].income
又有:barney.income = (* him).income = him - > income;
必须有(),因为 “ . ” 的优先级比 “ - > ”高;
结构体之间可以相互赋值;
结构中指针对字符串的操作
// names3.c -- use pointers and malloc()#include <stdio.h>#include <string.h> // for strcpy(), strlen()#include <stdlib.h> // for malloc(), free()struct namect { char * fname; // using pointers char * lname; int letters;};void getinfo(struct namect *); // allocates memoryvoid makeinfo(struct namect *);void showinfo(const struct namect *);void cleanup(struct namect *); // free memory when doneint main(void){ struct namect person; getinfo(&person); makeinfo(&person); showinfo(&person); cleanup(&person); return 0;}void getinfo (struct namect * pst){ char temp[81]; printf("Please enter your first name.\n"); gets(temp); // allocate memory to hold name pst->fname = (char *) malloc(strlen(temp) + 1); // copy name to allocated memory strcpy(pst->fname, temp); printf("Please enter your last name.\n"); gets(temp); pst->lname = (char *) malloc(strlen(temp) + 1); strcpy(pst->lname, temp);}void makeinfo (struct namect * pst){ pst->letters = strlen(pst->fname) + strlen(pst->lname);}void showinfo (const struct namect * pst){ printf("%s %s, your name contains %d letters.\n", pst->fname, pst->lname, pst->letters);}void cleanup(struct namect * pst){ free(pst->fname); free(pst->lname);}
/**********************************************************************************************************************************************************************************
枚举类型
使用关键字 enum enum常量通常为int型
/* enum.c -- uses enumerated values */#include <stdio.h>#include <string.h> // for strcmp()#include <stdbool.h> // C99 featureenum spectrum {red, orange, yellow, green, blue, violet};const char * colors[] = {"red", "orange", "yellow", "green", "blue", "violet"};#define LEN 30int main(void){ char choice[LEN]; enum spectrum color; bool color_is_found = false; puts("Enter a color (empty line to quit):"); while (gets(choice) != NULL && choice[0] != '\0') { for (color = red; color <= violet; color++) { if (strcmp(choice, colors[color]) == 0) { color_is_found = true; break; } } if (color_is_found) switch(color) { case red : puts("Roses are red."); break; case orange : puts("Poppies are orange."); break; case yellow : puts("Sunflowers are yellow."); break; case green : puts("Grass is green."); break; case blue : puts("Bluebells are blue."); break; case violet : puts("Violets are violet."); break; } else printf("I don't know about the color %s.\n", choice); color_is_found = false; puts("Next color, please (empty line to quit):"); } puts("Goodbye!"); return 0;}将程序中的颜色替换成相应的数字也是可以的,例如 red - > 0,orange - > 1(数字对应着结构中的相应位置的颜色,因为本身他们就是 int 型数据);
/*********************************************************************************************************************************************************************************
typedef
eg. typedef unsigned char BYTE;
该定义的作用于取决于该定义所存在的位置
what's more,typedef不同于define
eg.
#define char * stringstring name , sign;//只有name为char *//sign为char//因为define为文本替换//而 typedef与此不同typedef char * stringstring name , sign;//此时 name sign 均为char*当然typedef也可以用来定义一个结构体
typedef struct {double x;double y;} rect;rect r1={3.0;6.0};rect r2;r2=r1;
- C中的存储类、链接和内存管理
- C -- 存储类、链接和内存管理
- C语言存储类、链接和内存管理总结
- C 存储类/链接/内存管理
- 存储类,链接和内存管理
- 存储类、链接和内存管理复习
- 存储类、链接和内存管理
- 存储类,链接和内存管理
- 存储类、链接和内存管理
- 存储类、链接和内存管理
- 存储类、链接和内存管理
- 【C Primer Plus】第十二章 存储类、链接和内存管理
- C语言学习笔记1——存储类、链接和内存管理
- C primer plus第12章(存储类、链接和内存管理)
- C primer plus第12章(存储类、链接和内存管理)习题
- C Primer Plus第12章 存储类、链接和内存管理随笔
- 嵌入式软件开发培训笔记——C高级 存储类、链接和内存管理
- C学习笔记(八)存储类、链接和内存管理
- Spark Streaming的WindowedDStream
- POJ 1703 Find them, Catch them
- 2013年4月通信概论答案
- 免安装版Tomcat注册Windows服务
- Linux下.bz2与.gz文件的区别
- C中的存储类、链接和内存管理
- java中栈与队列的实现
- BMFont中文字体图集制作的方法~(for unity ngui)
- Unity3D UI层 两个摄像头会出现两层UI的情况
- Coder-Strike 2014 - Qualification Round A. Password Check
- UVA 11255 Necklace(Polya)
- C# 使用反射动态加载子窗体
- java 关于字节流和字符流的区别
- (code jam)Problem A. Magic Trick