当前位置:网站首页>变量(自动变量、静态变量、寄存器变量、外部变量)与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);
边栏推荐
- Constructing the implementation strategy of steam education for children
- Rotate menu 2.0
- Cordova Plugin /JPush PhoneGap 极光推送_本地推送_消息推送
- Notes to entry: do I need to know programming for O & M?
- Rotate menu 3.0
- How to realize the marketing purpose of small program integral mall
- Exec function of PHP
- Abbkine column exkine Pro animal cell / tissue Total Protein Extraction Kit
- C language -- 7 operators
- SQL Server2019安装的详细步骤实战记录(亲测可用)
猜你喜欢

【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas

SoC development environment and hardware development preparation
一次SQL查询优化原理分析(900W+数据从17s到300ms)

登堂入室之soc开发环境及硬件开发准备

Abbexa cdan1 siRNA instruction manual

Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)

Redis cache breakdown
An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)
php伪协议实现命令执行详情

Forward slash "/", backslash "\," escape character "\" and file path separator cannot be clearly remembered
随机推荐
How to realize the marketing purpose of small program integral mall
CentOS7环境下MySQL8常用命令小结
Abbexa cdan1 siRNA instruction manual
AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
SoC development environment and hardware development preparation
SQL Server row to column (pivot), column to row (unpivot)
Cordova plugin /jpush phonegap Aurora push_ Local push_ Message push
C language learning review -- 1 basic knowledge review
C language ---2 initial knowledge of data types
[nk] 牛客月赛51 G计算题
Leetcode advanced road - 169 Most elements
CCF class a conference or journal - regression related papers
Has the samesite cookie problem occurred when using identityserver?
Anti shake and throttling
Leetcode advanced path - reverse string
Apple zoom! It's done so well
Exec function of PHP
Leetcode advanced road - 69 Square root of X
Only three steps are needed to learn how to use low code thingjs to connect with Sen data Dix data
Redis cache avalanche