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

Applets: WSX scripts

shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)

Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank

MOOC Weng Kai C language week 7: array operation: 1. array operation 2. Search 3. preliminary sorting

Media set up live broadcast server

Remotely access the local website of services such as neo4j on the ECS

metasploit渗透ms7_010练习

DHCP principle and configuration

win下安装nessus

防火墙——iptables防火墙(四表五链、防火墙配置方法、匹配规则详解)
随机推荐
Three cache technologies -- localstorage, sessionstorage, cookies
Blue Bridge Cup square filling number
win下安装nessus
Result number of filled briquettes
Shell script - sort, uniq, TR, array sort, cut, Eval command configuration
Result fill in the blank (dfs*c language)
MOOC翁恺C语言第七周:数组运算:1.数组运算2.搜索3.排序初步
1、 PXE overview and installation
codesensor:将代码转化为ast后再转化为文本向量
Icc2 analysis timing artifact analyze_ design_ violations
[learning notes] VIM editor
VLAN configuration
Applet custom components - data, methods, and properties
Implementation method of Bert
Layer 3 switching and VRRP
Wechat applet custom compilation mode
静态和浮动路由
Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank
shell---循环语句练习
DOM -- event chain, event bubble and capture, event proxy