当前位置:网站首页>Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc
Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc
2022-06-10 22:11:00 【sinat_ forty-one million seven hundred and fifty-two thousand t】
C In language 4 Storage categories : Automatic variable (auto)、 Static variables (static)、 register (register)、 External variables (extern).
Automatic variable
It is usually defined in a custom function or code segment ( use {} The enclosed ) Variables defined , Are automatic variables , Also known as a local variable ( Except plus static Decorated variable ). The formal parameters of a function and the variables defined in the function are also automatic variables .
Automatic variable dynamic allocation of storage space , Data is stored in dynamic storage .
The storage space requested by the automatic variable will be automatically released at the end of the life cycle .
External variables
External variables are defined outside the function , The scope starts from the variable definition , To the end of this procedure .
Static variables
If you want the local variables in the function not to disappear after the function call ends , You can specify a local variable as a static local variable .
Register variables
To improve efficiency ,C Language allows the values of local variables to be stored in CPU In the register of , This kind of variable is called register variable , With keywords register Statement .
When using register variables, you need to pay attention to the following :
Only local automatic variables and formal parameters can be used as register variables ;
The number of registers in a computer system takes precedence , Cannot define any number of register variables ;
Address fetching operator cannot be used & Find the address of register variable .
Memory allocation
The following code is to open up space on the stack :
int val = 20;
char arr[10] = {0};The way to open up space on the stack has the following characteristics :
The size of the space opening is fixed ;
When an array is declared , You must specify the length of the array , The memory it needs is allocated at compile time .
The way to open up space on the stack can be seen as static memory allocation .
Dynamic memory allocation uses the following keywords :
malloc、free、calloc、realloc
malloc/free
malloc Like a memory request for a contiguous available space , Use as follows :
int *p = (int*)malloc(4); // Make a statement int Pointer to type ,free Release malloc Open up memory .
calloc/recalloc
void* calloc(size_t num,size_t size);calloc Also used for dynamic memory allocation , The function is for num Size is size The elements of open up a space , And initialize each byte of the space to 0.
calloc And malloc The difference is that calloc Each byte of the requested space will be initialized to... Before returning the address 0.
void* recalloc(void* ptr,size_t size);recalloc Adjust the size of dynamic development memory . On the basis of the original memory space , It will also move the data in the original memory to the new space .
recalloc There are two ways to adjust a space :
If there is enough space behind the original space , The expanded memory directly adds space after the original memory , The original spatial data does not change ;
If there is not enough space after the original space , In the heap space to find another suitable size of continuous space to use , This function returns a new memory address .
recalloc You should pay attention to memory leakage when using , as follows :
int* ptr = (int*)malloc(100);
if(ptr != NULL) {}
else {}
// Expand capacity
ptr = realloc(ptr,1000);
if(p != NULL) { ptr = p; }
else {}
free(ptr);
边栏推荐
- A WPF developed Print dialog box -printdialogx
- ThinkPHP v6.0. X deserialization vulnerability recurrence
- C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
- [C] overide can override the virtual method inherited from a higher parent
- 数组 求并集
- Rotate navigation bar
- [nk] 牛客月賽51 G計算題
- JS anchor positioning can extend many functions
- [qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?
- Principle of gravure overprint and factors affecting overprint
猜你喜欢

Abbexa AML1 DNA binding ELISA Kit instructions

What do software test engineers do?

oc swift 混编

如何激发文化创新的活力和驱动力

构建幼儿Steam教育实施策略
一次SQL查询优化原理分析(900W+数据从17s到300ms)

If else is too easy to use? (understanding this article will further improve your logic)

About type-C

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

C language -- 4 first-time constant
随机推荐
C language ---2 initial knowledge of data types
Super detailed tutorial for installing mysql8 in centos7 (no pit!)
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
Interpreting the new ecology of education in maker space
Ceph分布式存储集群Pool资源池的概念以及使用
The process of keyword search in Oracle tables
System reinstallation and system performance query
【C#】overide可重写从更高父级继承来的virtual方法
旋转菜单3.0
C language ---6 first knowledge of selection statement, loop statement, function and array
2022-06-09 RK817 PMU 電池溫度檢測
php的exec函数
Course design of imitation pottery ticket of wechat applet
登堂入室之soc开发环境及硬件开发准备
Qingniao Changping campus of Peking University: can I learn UI with a high school degree?
Exec function of PHP
I'm doing something cool
Which city should I go to after I graduate from it? Which position has a high salary? Which companies have good treatment?
PMP candidates, there are these places for PMP examination in Shenzhen in June 2022
Abbexa cell free DNA kit instructions