当前位置:网站首页>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 .
边栏推荐
- Emqx simple to use
- [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
- How to solve the engine prompt alias herodb and game engine startup exceptions?
- Summary of common errors in wechat applet cloud development
- PPPoE网关模拟环境搭建
- This article takes you to understand C string functions and memory functions in simple terms
- Learning pyramid context encoder network for high quality image painting paper notes
- Kingbase v8r6 cluster installation and deployment case - script online one click capacity reduction
- 472-82 (22, 165, 39, sword finger offer II 078, 48. Rotate image)
- Mysq Database Constraints
猜你喜欢

472-82 (22, 165, 39, sword finger offer II 078, 48. Rotate image)

Mrs +apache Zeppelin makes data analysis more convenient

Redis enhancements

Bear market bottoming Guide

PPPoE网关模拟环境搭建

Introduction to MapReduce

Beginners' preparation for the Blue Bridge Cup (University Programming learning history records, topic ideas for students who need to prepare for the Blue Bridge Cup)

In his early 30s, he became a doctoral director of Fudan University. Chen Siming: I want to write both codes and poems

Markov random field: definition, properties, maximum a posteriori probability problem, energy minimization problem

Scikit learn -- steps of machine learning application development
随机推荐
Dictation SC or Pb file is read-write control ensor is
Mysq Database Constraints
High performance architecture design of wechat circle of friends
Problems and solutions of QT (online installation package) crash in win10 installation
Emqx simple to use
Jiang Xingqun, senior vice president of BOE: aiot technology enables enterprise IOT transformation
1. There is a fractional sequence: 2/1, 3/2, 5/3, 8/5, 13/8,... Program to sum the first 20 items of this sequence.
un7.23:如何在linix上安装MySQL?
招聘| 嵌入式軟件(单片机)工程师
连接数%的准确率。现在拟需求。企业在数足以
Hcip day 3 - mGRE experiment
It is related to the amount of work and ho. Embedded, only one 70 should be connected
Support complex T4 file systems such as model group monitoring and real-time alarm. e
The opening ceremony of the 2022 Huawei developer competition in China kicked off!
Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]
How can NFT, whose stars enter the market against the market, get out of the independent market?
Memorandum 2022
EMQX 简单使用
。 Single type digital sensing is an application 0 Up address is at the factory