【C语言学习】《C Primer Plus》第12章 存储类、链接和内存管理

学习总结

 

1、作用域可分为代码块作用域、函数原型作用域或者文件作用域。

代码块作用域例子:

{

  for(int i=0;i<10;i++){  //C99允许

    …  //i的作用域

}

  ...

}

注:传统上,具有代码块作用域的变量必须在代码块的开始处进行声明,C99放宽这以规则,允许在一个代码中任何位置声明变量。

 

函数原型作用域例子:

int add(int a, int b);

a和b就是函数原型作用域的变量,在这函数内部都可以使用变量a和b,函数原型关注的是原型参数的类型,并非名字,所以在函数实现时,可以使用其他名字如:

int add(int c, int d){

  return c+b;

}

 

文件作用域例子:

#include <stdio.h>

int sum = 100;  //文件作用域

int add(int a, int b);

int main(){

  …

}

文件作用域相当于全局变量,在该文件内的左右地方都能使用。

2、链接分为外部链接、内部链接(static)、空连接。拥有外部链接和内部链接作用域的变量都具有静态存储时期,即程序执行期间一直存在。而拥有空连接作用的变量,都具有自动存储时期,当退出代码块时,分配的内存将被释放。

 

3、动态分配内存函数malloc(int n),n为申请内存字节数;calloc(int n,int m),n为申请内存单元数量,m为内存单元以字节计算的大小,malloc和calloc都是返回申请内存的起始指针,申请失败返回NULL;free((void *) p),p为释放的内存的指针。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(void){
 4         int *p;
 5         int i;
 6         p = malloc(5*sizeof(int));
 7         if(p==NULL){
 8                 exit(EXIT_FAILURE);
 9         }
10 
11         printf("EXIT_FAILURE=%d
",EXIT_FAILURE);
12 
13         for(i=0;i<5;i++){
14                 *p = i;
15                 p++;
16         }
17 
18         printf("p4=%d
",*--p);
19         printf("p3=%d
",*--p);
20         printf("p2=%d
",*--p);
21         printf("p1=%d
",*--p);
22         printf("p0=%d
",*--p);
23 
24         free(p);
25 
26         return 0;
27 }

运行结果:

EXIT_FAILURE=1

p4=4

p3=3

p2=2

p1=1

p0=0

4、free函数边界测试:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(void){
 4         int *p;
 5         int i;
 6 
 7         p = calloc(5,sizeof(int));
 8         if(p==NULL){
 9                 exit(EXIT_FAILURE);
10         }
11 
12         for(i==0;i<5;i++){
13                 *p = i;
14                 p++;
15         }
16         
17         //情况1:不做任何动作就回收
18         //p = p-4;  //情况2:指针不指向内存的第二个整数单元
19         //p = p-5;  //情况3:指针指回申请内存的第一个整数单元
20         printf("p=%d
",*p);
21         free(p);
22 
23         return 0;
24 }

执行结果:

情况1:报错

情况2:报错

情况3:正常

7、编程题(题8)

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int * make_array(int elem, int val);
 5 void show_array(const int ar[], int n);
 6 
 7 int main(void){
 8 
 9         int *pa;
10         int size;
11         int value;
12 
13         printf("Entet the number of elements:");
14         scanf("%d",&size);
15         while(size > 0){
16                 printf("Enter the initalization value:");
17                 scanf("%d",&value);
18                 pa = make_array(size, value);
19                 if(pa){
20                         show_array(pa,size);
21                         free(pa);
22                 }
23                 printf("Enter the number of elements(1<1 to quit):");
24                 scanf("%d",&size);
25         }
26 
27         printf("Done.
");
28 
29         return 0;
30 }
31 
32 int * make_array(int elem, int val){
33 
34         int i;
35         int *p;
36         p = (int *)malloc(elem*sizeof(int));
37 
38         for(i=0;i<elem;i++){
39                 p[i]=val;
40         }
41 
42         return p;
43 }
44 
45 void show_array(const int ar[], int n){
46 
47         int i;
48 
49         for(i=1;i<=n;i++){
50                 printf("%d",ar[i-1]);
51                 if(i!=0&&i%8==0)
52                         printf("
");
53                 if(i==n && i%8!=0)
54                         printf("
");
55         }
56 
57 }

执行结果:

Entet the number of elements:19

Enter the initalization value:2

22222222

22222222

222

Enter the number of elements(1<1 to quit):8

Enter the initalization value:2

22222222

Enter the number of elements(1<1 to quit):54

Enter the initalization value:4

44444444

44444444

44444444

44444444

44444444

44444444

444444

Enter the number of elements(1<1 to quit):0

Done.

原文地址:https://www.cnblogs.com/wcd144140/p/4755701.html