当前位置:网站首页>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 .
边栏推荐
- 《Just because》阅读感受
- UE4 UI自适应屏幕
- Oriental Aesthetics and software design
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- Technological Entrepreneurship: failure is not success, but reflection is
- Introduction to victoriametrics
- 服务可见可观测性
- 分享一下如何制作专业的手绘电子地图
- Servicemesh mainly solves three pain points
- U++ 学习笔记 堆
猜你喜欢
Hanoi Tower problem
Image segmentation using pixellib
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
图像基础概念与YUV/RGB深入理解
Landingsite eband B1 smoke test case
phpcms实现订单直接支付宝支付功能
#include<>和#include“”的区别
Gee: (II) resampling the image
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
随机推荐
Technical solution of vision and manipulator calibration system
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
Unity3D学习笔记4——创建Mesh高级接口
C language, to achieve three chess games
《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
APP页面分享口令Rails实现
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Five message formats of OSPF
Error in PIP installation WHL file: error: is not a supported wheel on this platform
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
540. Single element in ordered array
Phpcms realizes the direct Alipay payment function of orders
[leetcode] sword finger offer 04 Search in two-dimensional array
An overview of the development of affective computing and understanding research
Introduction to the principle of geographical detector
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
Market Research - current situation and future development trend of herringbone gear Market
Official announcement! The golden decade of new programmers and developers was officially released