当前位置:网站首页>变量(自动变量、静态变量、寄存器变量、外部变量)与C的内存分配malloc/free、calloc/recalloc
变量(自动变量、静态变量、寄存器变量、外部变量)与C的内存分配malloc/free、calloc/recalloc
2022-06-10 20:50:00 【sinat_41752325】
C语言中的4种存储类别:自动变量(auto)、静态变量(static)、寄存器(register)、外部变量(extern)。
自动变量
通常定义在自定义函数内或代码段(用{}括起来的)定义的变量,都是自动变量,也称为局部变量(除了加static修饰的变量)。函数的形参和在函数中定义的变量也属于自动变量。
自动变量动态分配存储空间,数据存储在动态存储区中。
自动变量所申请的存储空间在结束生命周期时会自动释放这些空间。
外部变量
外部变量是在函数的外部定义的,作用域从变量定义处开始,到本程序文件的末尾。
静态变量
如果希望函数中的局部变量在函数调用结束后不消失,可以指定局部变量为静态局部变量。
寄存器变量
为提高效率,C语言允许将局部变量的值存放在CPU的寄存器中,这种变量叫做寄存器变量,用关键字register声明。
使用寄存器变量时需要注意以下:
只有局部自动变量和形式参数可以作为寄存器变量;
一个计算机系统的寄存器数目优先,不能定义任意多个寄存器变量;
不能使用取地址运算符&求寄存器变量的地址。
内存分配
如下代码是在栈上开辟空间:
int val = 20;
char arr[10] = {0};在栈上开辟空间的方式有以下特点:
空间开辟大小是固定的;
数组在声明的时候,必须指定数组的长度,它所需要的内存在编译时分配。
栈上开辟空间的方式可以看作是静态内存分配。
动态内存分配用到如下关键字:
malloc、free、calloc、realloc
malloc/free
malloc像内存申请一块连续可用的空间,使用时如下:
int *p = (int*)malloc(4); // 声明一个int类型的指针,free释放malloc开辟的内存。
calloc/recalloc
void* calloc(size_t num,size_t size);calloc也用来动态内存分配,函数的功能是为num个大小为size的元素开辟一块空间,并且把空间的每个字节初始化为0。
calloc与malloc的区别在于calloc会在返回地址之前把申请的空间的每个字节全部初始化为0。
void* recalloc(void* ptr,size_t size);recalloc调整动态开辟内存的大小。在原内存空间大小的基础上,还会将原来内存中的数据移动到新空间。
recalloc调整空间有两种情况:
如果原有空间之后有足够大的空间,扩展内存就在原有内存之后直接追加空间,原来的空间的数据不发生变化;
如果原有空间之后没有足够多的空间,在堆空间上另找一个合适大小的连续空间来使用,这样函数返回的是一个新的内存地址。
recalloc使用时需要注意内存泄漏的问题,如下:
int* ptr = (int*)malloc(100);
if(ptr != NULL) {}
else {}
// 扩展容量
ptr = realloc(ptr,1000);
if(p != NULL) { ptr = p; }
else {}
free(ptr);
边栏推荐
- 自媒体视频创作如何选择领域?
- 标配双安全气囊,价格屠夫长安Lumin 4.89万起售
- Leetcode advanced road - 167 Sum of two numbers II - input ordered array
- php伪协议实现命令执行详情
- H. Relationship among Nalu, RBSP and sodb in 264
- [nk] 牛客月赛51 G计算题
- How to choose the field of we media video creation?
- Can I make up the exam if I fail the soft exam? Here comes the answer
- How to view the occupied space of a table in MySQL database
- Constructing the implementation strategy of steam education for children
猜你喜欢

Acl2022 | bert2bert: an efficient pre training method of parameter reuse, which significantly reduces the training cost of oversized models

初中毕业生,选择中职学校也可以升入高等学府

2022-06-09 RK817 PMU 电池温度检测

Which city should I go to after I graduate from it? Which position has a high salary? Which companies have good treatment?

JS mobile terminal copy text to clipboard code

Understanding of related concepts of target detection

Calculus review 1

Redis cache breakdown

C language -- 4 first-time constant

Abbexa cell free DNA kit instructions
随机推荐
构建幼儿Steam教育实施策略
Abbexa AML1 DNA binding ELISA Kit instructions
Leetcode advanced road - 136 A number that appears only once
PHP pseudo protocol implementation command execution details
旋转菜单3.0
[nk] Niuke monthly competition 51 f-average question
System reinstallation and system performance query
报错解决Error parsing Mapper XML
Video monitoring system storage control, bandwidth calculation method
在模仿学习中进步的智能机器人
Extracting Nalu unit data from H264 real-time stream
Principle of gravure overprint and factors affecting overprint
2021年平均工资出炉,IT行业不出所料
SQL Server2019安装的详细步骤实战记录(亲测可用)
. Net open source free lunch is over?
NFT版权/版税
As a programmer, is it really that important for the underlying principles?
登堂入室之soc开发环境及硬件开发准备
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)
CentOS7环境下MySQL8常用命令小结