当前位置:网站首页>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);
边栏推荐
猜你喜欢

JS anchor positioning can extend many functions

Naturalspeech model synthetic speech achieves human speech level for the first time in CMOS test

Abbexa AML1 DNA binding ELISA Kit instructions

2022 - 06 - 09 rk817 PMU Battery Temperature Detection

【MySQL】常见数据类型总结

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

2022-06-09 rk817 PMU battery temperature detection
![[nk] 牛客月赛51 F-平均题](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)
[nk] 牛客月赛51 F-平均题

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

Shaping teenagers' comprehension ability with children's programming thinking
随机推荐
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
2022-06-09 rk817 PMU battery temperature detection
报错解决Error parsing Mapper XML
19 MySQL optimizations commonly used in projects
登堂入室之soc开发环境及硬件开发准备
php的exec函数
Before we learn about high-performance computing, let's take a look at its history
数组 删除数组中的重复项
Principle of gravure overprint and factors affecting overprint
Detailed explanation of MySQL range query optimization scenario instances
Mysql的回表查询?如何避免?
How to choose the field of we media video creation?
Acl2022 | bert2bert: an efficient pre training method of parameter reuse, which significantly reduces the training cost of oversized models
【MySQL】表的约束
数组 是否存在重复元素
Abbexa cell free DNA kit instructions
Shaping teenagers' comprehension ability with children's programming thinking
The system needs to scan all the files and try to identify the cover of the video
Tableau auto - fabriqué
Notes to entry: do I need to know programming for O & M?