当前位置:网站首页>Dynamic memory allocation (malloc calloc realloc free)
Dynamic memory allocation (malloc calloc realloc free)
2022-07-02 22:32:00 【Hao Shaokang】
Memory is mainly divided into three parts , The stack area , Heap area , Static zone , The memory space created by the dynamic memory function is in the heap .
The stack area : local variable , Function parameter
Heap area : Dynamic memory allocation ,(malloc,calloc, realloc, free)
Static zone : Global variables , Static variables
malloc function :
malloc If the function opens space successfully , Then return the starting address of that space , If opening up space fails ( Possible causes : There is too much room to open up ), Then return to NULL, So check the returned pointer ,
When opening up space is successful , And after use , This space should be released as soon as possible ( Return this space to the operating system ), application free() Function frees up space , After release p It's a wild pointer , In order to avoid passing later p Illegal access to memory , Should put p Set as NULL
calloc function :
calloc After opening up space , The space content will be initialized to 0
realloc function :
realloc Functions can be divided into three cases :
If realloc The first argument to the function is NULL, Then the function is equivalent to malloc
Here are some common errors in dynamic memory development :
1. Dereference of null pointer :
2. Cross border access to dynamic open space :
3. The release of non dynamic development space
4. Release a part of dynamic development memory
5. Multiple releases of the same dynamic memory
6. Dynamic memory forget to release ( Memory leak )
Flexible array :
1. At least one other member in front of the flexible array member in the structure
2.sizeof Return the size of this structure, excluding the memory of the flexible array
3. Structures that contain flexible array members use malloc() Function to dynamically allocate memory , And the allocated memory should be larger than the size of the structure , To fit the expected size of the flexible array .
边栏推荐
- "New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
- The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
- Using emqx cloud to realize one machine one secret verification of IOT devices
- It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
- 关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
- 一周生活
- [C question set] of V
- Unity3d learning notes 4 - create mesh advanced interface
- Une semaine de vie
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
猜你喜欢
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
[shutter] shutter resource file use (import resource pictures | use image resources)
Daily book -- analyze the pain points of software automation from simple to deep
PIP audit: a powerful security vulnerability scanning tool
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
Reading experience of just because
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
540. Single element in ordered array
随机推荐
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Market Research - current situation and future development trend of herringbone gear Market
20220702-程序员如何构建知识体系?
一周生活
[shutter] shutter gesture interaction (small ball following the movement of fingers)
PHP wechat red packet grabbing algorithm
C language, to achieve three chess games
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
Market Research - current situation and future development trend of environmental friendly fireworks Market
An overview of the development of affective computing and understanding research
ArrayList analysis 2: pits in ITR, listiterator, and sublist
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
TinyMCE visual editor adds Baidu map plug-in
攻防世界pwn题:Recho
Market Research - current market situation and future development trend of high tibial osteotomy plate
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
Market Research - current market situation and future development trend of third-party data platform
100 important knowledge points that SQL must master: management transaction processing
Une semaine de vie
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba