当前位置:网站首页>Standard C language learning summary 8
Standard C language learning summary 8
2022-07-28 07:08:00 【c7473168】
One , What is heap memory
Is a memory segment of the process (text\data\bss\heap\stack), Manually managed by the programmer
Enough advantages , Disadvantages: it is troublesome to use
Two , Why use heap memory
1, With the complexity of the program, the amount of data becomes more
2, The application release of other memory segments is uncontrolled , The requested release of heap memory is controlled
It can save memory in time
3、 ... and , How to use heap memory
Be careful :C There are no statements in the language that control heap memory , Only use C Functions in the standard library
#include <stdlib.h>
void *malloc(size_t size);
function : Request from heap memory size The value of the data in memory applied for by bytes of memory is uncertain
Return value : Successfully return the first address of the requested continuous memory , Failure to return NULL
void free(void *ptr);
function : Free a heap memory
ptr: The first address of heap memory to be released
Be careful :free Release is just permission , The data will not be cleaned up
Be careful , Cannot release continuously , But it can be released NULL
void *calloc(size_t nmemb, size_t size);
Function requests from heap memory nmemb block , each size Byte size memory
Return value : Successfully return the first address of the requested continuous memory , Failure to return NULL
calloc(10,4) == malloc(40)
Be careful :calloc The requested memory will be initialized to 0, Velocity ratio malloc slow
void *realloc(void *ptr, size_t size);
function : Change the size of the existing heap memory ,size Indicates the adjusted size
On the original basis, raise the major and adjust the minor
Return value : The novice who adjusted the memory block stared , Be sure to accept the return value again , It may not be adjusted in the original position
If it cannot be adjusted in the original position
1, Apply for a new compound size memory
2, Copy the data in the original memory
3, Free original memory , Returns the first address of the new memory
Four ,malloc Memory management mechanism '
1, When the first time to malloc Application memory ,malloc Will request memory from the operating system , The operating system will directly allocate 33 page
(1 page =4096 byte ) Give the memory to malloc management , But it doesn't mean that you can cross the border , because malloc
Other memory may be allocated to " others ", This will produce dirty data
2, There will be gaps between each memory block (4~12 byte ), Part of the gap is for memory alignment , There must be 4 byte
Recorded malloc The maintenance information determines the next malloc Where to allocate memory
If the maintenance information is destroyed , Will affect the next time malloc perhaps free The process of ,'
5、 ... and , Problems needing attention when using heap memory
Memory leak :
Memory can no longer be used , And can't be released , When you need to use it again, you can only reapply memory ,
Then continue to repeat the above process , Over time, less and less memory is available
Be careful : Once the process ends, all resources belonging to the process will be recycled by the operating system
How to avoid memory leakage as much as possible :
Whoever applies for memory will release , Who knows who should be released
How to determine , Locating memory leaks :
1, View memory usage
win Task manager
Linux ps -aux command
2, With the help of some code analysis tools matrace Check malloc and free Whether they appear in pairs
3, encapsulation malloc,free, Record request , Release the information into the log file
Memory fragments :
Memory that has been released but can no longer be used is called memory fragmentation , Due to the incongruity between the time of application and release
It cannot be completely avoided, but can only be minimized
How to reduce memory fragmentation :
1, Try to use stack memory
2, Don't apply frequently , Free memory
3, Try to apply for large blocks of memory by yourself
6、 ... and , Memory cleaning function
#include <strings.h>
void bzero(void *s,size_t n);
function : Clear a piece of memory into 0
s: The first address of the memory block
n: Number of bytes of memory block
#include <string.h>
voidd *memset(void *c,size_t n);
function : Set the memory block as c
s: The first address of the memory block
c: What you want to set up ASCII value
n The number of bytes in memory
Return value : The first memory address after successful setting
7、 ... and , How to define a two-dimensional array with heap memory
Pointer array
Definition n That's ok ,m Column 2D array
type * arr[n];
for(int i=0;i<n;i++)
{
arr[i] = malloc(m*sizeof( type ));
}
Be careful : In every line m Values can be different , In this way, irregular two-dimensional arrays can be defined
shortcoming : Application trouble , It is easy to generate memory fragmentation
advantage : It can be irregular , Low memory requirements
Array pointer
type (*arrp)[n] = malloc(sizeof( type )*n*m);
apply m That's ok n Two dimensional array of columns
shortcoming : High requirements for continuous memory , Maybe the application failed
advantage : The application is simple
Be careful : The so-called multidimensional array is actually simulated by one-dimensional array
practice 1: To calculate the 100~10000 Between all the prime numbers , The results are stored in heap memory , Try not to waste memory
1, First calculate how many , Apply for all in the next
2, While calculating , Side application
边栏推荐
猜你喜欢

PXE无人值守安装管理

VNC Timed out waiting for a response from the computer

Shell script - sort, uniq, TR, array sort, cut, Eval command configuration

kali下安装nessus

joern运行后.joernindex数据库无内容

codesensor:将代码转化为ast后再转化为文本向量

Joern's code uses -devign

Custom component -- pure data field & component life cycle

Results fill in the blank shopping list (teach you to solve it with Excel)

Custom components -- styles
随机推荐
[learning records of erudite Valley] Super summary, attentive sharing | collection
easypoi导出表格带echars图表
[learning notes] linked list operation
Repair the faulty sector
Shell--第一天作业
Svg understanding and drawing application
easypoi导出隔行样式设置
Three cache technologies -- localstorage, sessionstorage, cookies
小甲鱼C(第五章循环控制结构程序567)break和continue语句
静态和浮动路由
Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
MOOC翁恺C语言第五周:1.循环控制2.多重循环3.循环应用
MySQL查询父节点下面的所有子孙节点,查询用户列表时多级(公司)部门处理,根据反射,递归树形结构工具类
kali下安装nessus
MySQL排除节假日,计算日期差
Operation document tree
Custom component -- data listener
Canvas drawing 2
DHCP服务
Uniapp monitor whether the app has a network connection