C primer plus自用知识点整理(第十二章)存储类别、链接和内存管理
发布日期:2022-01-20 01:07:13
浏览次数:1
分类:技术文章
本文共 3924 字,大约阅读时间需要 13 分钟。
书籍整理内容:
最近在看C primer plus(加深巩固自己的C语言技巧,为以后学习C++打个基础)。 里面知识针对自己以后要查的点整理出来。 使用工具:visual studio 2013 :https://blog.csdn.net/answerMack/article/details/103766020 :https://blog.csdn.net/answerMack/article/details/103805900 :https://blog.csdn.net/answerMack/article/details/103855794 :https://blog.csdn.net/answerMack/article/details/103870182 https://blog.csdn.net/answerMack/article/details/103891048 https://blog.csdn.net/answerMack/article/details/103953376 https://blog.csdn.net/answerMack/article/details/103978471 :https://blog.csdn.net/answerMack/article/details/104114028 :https://blog.csdn.net/answerMack/article/details/105222269目录
本章主要讲解概念、基本都是函数的文字讲解。注意一下就好,易困!!
作用域
作用域描述程序中可访问标识符的区域。
(1)块作用域: (2)函数作用域 (3)函数原型作用域 (4)文件作用域 (6)翻译单元和文件链接
c变量的属性:外部链接、内部链接、无链接
存储期
存储类别
自动变量
寄存器变量
块作用域静态变量
外部链接静态变量
定义变量、声明变量
内部链接静态变量
多文件
存储类别说明符
auto
register static extern _Thread_local typedef 例子:// parta.c --- various storage classes// compile with partb.c#includevoid report_count();void accumulate(int k);int count = 0; // file scope, external linkageint main(void){ int value; // automatic variable register int i; // register variable printf_s("Enter a positive integer (0 to quit): "); while (scanf_s("%d", &value) == 1 && value > 0) { ++count; // use file scope variable for (i = value; i >= 0; i--) accumulate(i); printf_s("Enter a positive integer (0 to quit): "); } report_count(); return 0;}void report_count(){ printf_s("Loop executed %d times\n", count);}
// partb.c -- rest of the program// compile with parta.c#includeextern int count; // reference declaration, external linkagestatic int total = 0; // static definition, internal linkagevoid accumulate(int k); // prototypevoid accumulate(int k) // k has block scope, no linkage{ static int subtotal = 0; // static, no linkage if (k <= 0) { printf_s("loop cycle: %d\n", count); printf_s("subtotal: %d; total: %d\n", subtotal, total); subtotal = 0; } else { subtotal += k; total += k; }}
存储类别和函数
随机数函数和静态变量
掷骰子
分配内存:malloc()和free()
3种创建数组的方法
/* dyn_arr.c -- dynamically allocated array */#include#include /* for malloc(), free() */int main(void){ double * ptd; int max; int number; int i = 0; puts("What is the maximum number of type double entries?"); if (scanf_s("%d", &max) != 1) { puts("Number not correctly entered -- bye."); exit(EXIT_FAILURE); } ptd = (double *)malloc(max * sizeof(double)); if (ptd == NULL) { puts("Memory allocation failed. Goodbye."); exit(EXIT_FAILURE); } /* ptd now points to an array of max elements */ puts("Enter the values (q to quit):"); while (i < max && scanf_s("%lf", &ptd[i]) == 1) ++i; printf_s("Here are your %d entries:\n", number = i); for (i = 0; i < number; i++) { printf_s("%7.2f ", ptd[i]); if (i % 7 == 6) putchar('\n'); } if (i % 7 != 0) putchar('\n'); puts("Done."); free(ptd); return 0;}
动态分配内存:
free()的重要性
calloc函数
动态内存分配和变长数组
动态内存分配和存储类别
#include#include #include int static_store = 30;const char * pcg = "String Literal";int main(){ int auto_store = 40; char auto_string[] = "Auto char Array"; int * pi; char * pcl; pi = (int *)malloc(sizeof(int)); *pi = 35; pcl = (char *)malloc(strlen("Dynamic String") + 1); strcpy_s(pcl, strlen("Dynamic String")+1,"Dynamic String"); printf_s("static_store: %d at %p\n", static_store, &static_store); printf_s(" auto_store: %d at %p\n", auto_store, &auto_store); printf_s(" *pi: %d at %p\n", *pi, pi); printf_s(" %s at %p\n", pcg, pcg); printf_s(" %s at %p\n", auto_string, auto_string); printf_s(" %s at %p\n", pcl, pcl); printf_s(" %s at %p\n", "Quoted String", "Quoted String"); free(pi); free(pcl); return 0;}
ANSI C类型限定符
const、volatile、restrict、_Atomic+stdatomic.h库文件
const(第4章、第10章)
指针和形参声明中使用const
volatile类型限定符
restrict类型限定符
_Atomic类型限定符
并行程序:
旧关键字的新位置
关键概念
本章小结
2020-04-08
大连转载地址:https://blog.csdn.net/answerMack/article/details/105338533 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年03月22日 22时03分13秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
TensorFlow(keras)入门课程--05 复杂图像处理
2019-04-26
TensorFlow(keras)入门课程--06 CNN用于猫狗数据集
2019-04-26
【比赛篇】TinyMind人民币面值 - 热身赛
2019-04-26
卷积神经网络交通标志识别
2019-04-26
【实战】深度学习构建人脸面部表情识别系统
2019-04-26
Python机器学习&数据分析-关联规则
2019-04-26
【实战】kaggle猫狗大战-卷积神经网络实现猫狗识别
2019-04-26
01 计算机视觉-opencv图像基本操作
2019-04-26
02 计算机视觉-opencv阈值与滤波处理
2019-04-26
03 计算机视觉-opencv图像形态学处理
2019-04-26
04计算机视觉-opencv图像梯度处理
2019-04-26
05计算机视觉-opencv边缘检测
2019-04-26
07 计算机视觉-opencv模板匹配
2019-04-26
Word2vec与logging模块_完整代码_CodingPark编程公园
2019-04-26
Auto-Encoder&VAE_完整代码_CodingPark编程公园
2019-04-26
Debug讲解_CodingPark编程公园
2019-04-26
命名实体识别_利用CRF_CodingPark编程公园
2019-04-26
Pycharm使用-取消pytest的办法_CodingPark编程公园
2019-04-26
OpenCV图像处理基础_CodingPark编程公园
2019-04-26