当前位置:网站首页>C language memory function
C language memory function
2022-07-03 14:36:00 【roseisbule】
c Language has functions to operate memory , Respectively :malloc,realloc,calloc,free.
Why should there be memory functions ?
In terms of personal feelings , When developing an address book , Everyone's address book is different , This makes it difficult to determine how much memory you need to create for the structure array . If you do not use memory functions , Then simply and roughly set the structure array to be very large , It's hard not to waste memory .
malloc:
void* malloc (size_t size);
1.malloc The function is responsible for applying for a continuous space from memory , If the application is successful , Then return the address of the space , If the application fails , Then return to NULL.
2. Because I don't know what type you want , therefore malloc The return is void* The pointer to .
3. If malloc Transmitted size_t Parameter is 0, What value is returned ,c The language standard is not defined , Depends on the compiler .
calloc:
void* calloc (size_t num, size_t size);
1.calloc and malloc equally , It is also to apply for a continuous memory space , Successful application , The return address , Failure , return NULL.
2.calloc It's an application num individual size Size space .
3.calloc Function after successful application , Each byte of this space will be initialized to 0.
realloc:
void* realloc (void* ptr,size_t size);
1.realloc Used to adjust the dynamic memory size . If the rear memory is enough to accommodate the new memory size , be realloc Will be in ptr After adding , Make the memory become size size . But if it's not enough ,realloc Will find a piece of memory again , The memory address will change .
2.realloc Function if it fails , Returns the NULL, If successful, return the address . Because I don't know whether it will succeed , So it's using realloc When , Can't directly realloc The return value of is assigned to ptr, To create an intermediate pointer , If the middle pointer is not NULL, Then assign the middle pointer to ptr.
free:
void free (void* ptr);
1.free Function is used to reclaim dynamic memory .
2.c Language standards are not defined , When to ptr What should I do when the address of is not the address opened by dynamic memory , It depends on the compiler .
3. If ptr by NULL, be free Functions do not produce behavior .
边栏推荐
- Zzuli:1049 square sum and cubic sum
- 7-2 and then what time (15 minutes)
- Preliminary summary of structure
- Zzuli:1042 sum of sequence 3
- puzzle(016.4)多米诺效应
- Time conversion ()
- 论文分享:Generating Playful Palettes from Images
- Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
- Niuke: crossing the river
- Too many files with unapproved license
猜你喜欢
ConstraintLayout 的使用
Adc128s022 ADC Verilog design and Implementation
7-18 finding the single root of polynomial by dichotomy
使用并行可微模拟加速策略学习
编程语言:类型系统的本质
Showmebug entered Tencent conference, opening the era of professional technical interview
Why is this error reported when modifying records in the database
pyQt界面制作(登录+跳转页面)
NPM install is stuck with various strange errors of node NPY
论文分享:Generating Playful Palettes from Images
随机推荐
Zzuli: sum of 1051 square roots
Mongodb index
puzzle(016.3)千丝万缕
Zzuli:1052 sum of sequence 4
7-10 stack of hats (25 points) (C language solution)
String reverse order
Puzzle (016.3) is inextricably linked
How Facebook moves instagram from AWS to its own server
Stop asking yourself if you are suitable for software testing
7-17 crawling worms (break exercise)
Dllexport and dllimport
Strategy, tactics (and OKR)
洛谷P5194 [USACO05DEC]Scales S 题解
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
retrofit
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
【7.3】146. LRU caching mechanism
7-14 sum integer segments (C language)
使用并行可微模拟加速策略学习
Tonybot humanoid robot infrared remote control play 0630