当前位置:网站首页>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 .

边栏推荐
- 2019 Nanchang (relive the classic)
- 【leetcode】1380. Lucky number in matrix
- Web侧防御指南
- Market Research - current market situation and future development trend of third-party data platform
- How to write a good program when a big book speaks every day?
- 100 important knowledge points that SQL must master: using cursors
- 攻防世界pwn题:Recho
- From personal heroes to versatile developers, the era of programmer 3.0 is coming
- Pointer array parameter passing, pointer parameter passing
- Market Research - current market situation and future development trend of total nutrition products
猜你喜欢

Sql service intercepts string

What is it that makes you tremble? Those without fans can learn

【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)

Official announcement! The golden decade of new programmers and developers was officially released

Oriental Aesthetics and software design

540. Single element in ordered array
![[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)](/img/4c/c8dae41fc2eb18b5153cf36861fc7d.jpg)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
![[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)](/img/f7/cb41d159e5c5ef3f4f1b9468d52ccc.jpg)
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)

TinyMCE visual editor adds Baidu map plug-in

Pointer and string
随机推荐
Regular expression (2)
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
LightGBM原理及天文数据中的应用
TinyMCE visual editor adds Baidu map plug-in
Service visibility and observability
Technical solution of vision and manipulator calibration system
Web side defense Guide
Market Research - current market situation and future development trend of aircraft audio control panel system
SQL必需掌握的100个重要知识点:管理事务处理
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
【leetcode】1380. Lucky number in matrix
Infrastructure is code: a change is coming
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
UE4 UI自适应屏幕
Micro service gateway selection, please accept my knees!
Servicemesh mainly solves three pain points
New feature of go1.18: trylock, which has been tossed n times
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】