当前位置:网站首页>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 .
边栏推荐
- Add ZABBIX calculation type itemcalculated items
- The mail function of LNMP environment cannot send mail
- 556. 下一个更大元素 III : 简单构造模拟题
- Strategy, tactics (and OKR)
- Puzzle (016.4) domino effect
- Dllexport et dllimport
- MySQL multi table query subquery
- [qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
- SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
- 【7.3】146. LRU缓存机制
猜你喜欢

Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue

Paper sharing: generating playful palettes from images

Protobuf and grpc

Mysql多表查询 #子查询

分布式事务(Seata) 四大模式详解

使用并行可微模拟加速策略学习

7-18 finding the single root of polynomial by dichotomy

Happy capital new dual currency fund nearly 4billion yuan completed its first account closing

Accelerating strategy learning using parallel differentiable simulation

Code writing and playing method of tonybot humanoid robot at fixed distance
随机推荐
Strategy, tactics (and OKR)
pyQt界面制作(登录+跳转页面)
适用于XP的DDK
7-3 count the number of words in a line of text
Adc128s022 ADC Verilog design and Implementation
Zzuli:1048 factorial table
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Zzuli:1058 solving inequalities
【7.3】146. LRU缓存机制
Address book sorting
X86 assembly language - Notes from real mode to protected mode
556. 下一个更大元素 III : 简单构造模拟题
Preliminary summary of structure
Thread.sleep和TimeUnit.SECONDS.sleep的区别
关于敏捷的一些概念
Time conversion ()
puzzle(016.4)多米诺效应
Optical cat super account password and broadband account password acquisition
7-24 reduction of the simplest fraction (rolling Division)
Mongodb index