当前位置:网站首页>Pointer learning diary (III)
Pointer learning diary (III)
2022-07-24 05:14:00 【Rain and cold at night in Xiaoxiang】
Dynamic memory allocation
When an array is declared , The memory he needs is allocated at compile time .
But we always write similar code :
int a[1000000] = { 0 };
In a sense , you 're right , And this kind of method can well avoid the problem of array overflow .
But at the same time , This is definitely not a good program , The reason is simple , Memory helps you allocate sizeof(int)*1000000;
But you may not use a lot of memory at all , This creates waste . Fortunately , stay C and C++ Programmers can manually allocate memory and free unused memory .
One 、malloc And free
Both are in the header file stdlib.h In a statement .
malloc Extract a suitable piece of memory from the memory pool , And return a Pointer to this memory .
When a previously allocated memory is no longer used , Program call free Function returns it to the memory pool for future needs .
ps:malloc Allocated is a continuous block of memory , If the available memory of the memory pool can meet this demand ,malloc Just Returns a pointer to the starting position of the allocated memory block . If the memory pool is empty , It will ask the system to get more memory , If the system cannot provide more memory , that Will return a NULL The pointer
therefore !! For each slave malloc All returned pointers should be checked , Make sure it's not NULL, It's very important .
ps:C++ Medium new It may be more convenient , But this study diary mainly introduces C, So I won't retell too much here
void *malloc(size_t size);
void free(void *pointer);
malloc Return to one void The pointer , That's why . Standard represents a void Pointers of type can be converted to pointers of any other type .
Two 、calloc And realloc
calloc Also used to allocate memory , And malloc The biggest difference is ,calloc Initialize it to... Before returning a pointer to memory 0.relloc Function is used to modify the size of a previously allocated memory block , You can expand or reduce a piece of memory
void *calloc(size_t num_elements,size_t element_size);
void relloc(void *ptr,size_t new_size)
Pass to free The pointer to must be a malloc、calloc or realloc The pointer returned by the function .
!! Be careful , Don't visit already free Memory freed by function . The most common situation is , A pointer to dynamically allocated memory has been copied , Then access the copy in other parts of the program .
边栏推荐
- 招聘| 嵌入式軟件(单片机)工程师
- Web3 product manager's Guide: how to face the encryption world
- Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
- The network NN can calculate the NTCP provided by the host system
- 浅谈不可转让的声誉积分NFT SBTs面临的困境
- The second chapter is encog's data acquisition
- Image to image translation with conditional advantageous networks paper notes
- MS simulated written test
- MapReduce介绍
- Theoretical basis of machine learning
猜你喜欢

Drools 开发决策表
![[Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket](/img/60/23fc79cf0e399265b4bd75159ad4d1.png)
[Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket

Event extraction and documentation (2020-2021)

pso和mfpso

Kingbase V8R6集群安装部署案例---脚本在线一键扩容

How can NFT, whose stars enter the market against the market, get out of the independent market?

The difference between run and start in thread class

Heavy! The 2022 China open source development blue book was officially released

PSO and mfpso

Token of space renewable energy
随机推荐
Recruitment | embedded software (MCU) Engineer
Knowledge record of College Physics C in advance in summer [update]
The second chapter is encog's data acquisition
The network NN can calculate the NTCP provided by the host system
明星逆市入局的NFT,如何能走出独立行情?
Icml2022 | rock: causal reasoning principle on common sense causality
High performance architecture design of wechat circle of friends
Scikit learn -- steps of machine learning application development
Transpose of array sparse matrix
Drools development decision table
The world's first large aerospace model came out. Wenxin's second supplement "Fuchun Mountain Residence map" is Baidu Pratt Whitney AI's perseverance
Blue Bridge Cup 31 day sprint 21 day (C language)
Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]
Chiitoitsu(期望dp)
Context encoders: feature learning by painting paper notes
Jetson设备 faild to download repository information使用小技巧记录
网NN计算能主机系统资e提供的NTCP
Execution sequence of finally and return
472-82 (22, 165, 39, sword finger offer II 078, 48. Rotate image)
Summary of the development process and key and difficult points of the Listening Project