当前位置:网站首页>Dynamic memory allocation
Dynamic memory allocation
2022-06-25 14:58:00 【--988】
Catalog
| The stack area | local variable 、 The formal parameter of the function |
| Heap area | Dynamic memory allocation (malloc,free,calloc,realloc) |
Static zone | Global variables 、 Static variables |
One 、malloc
form : void* malloc(size_t size)
//C99 Yes, variable length arrays can be created , Not all compilers support .gcc test_std=c99
#include<stdio.h> //visual Compiler does not support , but gcc Compiler support
#include<string.h>
int mian()
{
int n=0;
scanf("%d",&n);
int arr[n];
int i=0;
for(i=0;i<n;i++)
{arr[i]=i;
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
return 0;
}#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<stdio.h>
int mian()
{
int *p=(int* )malloc(10*sizeof(int));// Integer pointer maintenance . Apply to memory 10 An integer space
if(p==NULL)
{
printf("%s\n",strerror(errno));// One way to print the cause of the error , The requested space exceeds the memory space
}
else
{
int i=0;
for(i=0;i<10;i++)
{
*(p+i)=i;
}
for(i=0;i<10;i++)
{
printf("%d ",*(p+i);
}
}
free(p)
return 0;
}Explain the above code : This function applies to memory for a contiguous block of free space , And return the pointer to this space
- If the development is successful , Then return a pointer to open a good space
- If the development fails , Returns a NULL Pointer therefore malloc The return value of must be checked
- The return value type is void*, therefore malloc Function doesn't know the type of open space , It is up to the user to decide when using
- If parameters size by 0,malloc Behavior is defined by standards , Depends on the compiler ,free Function is used to release memory opened dynamically
- If parameters ptr The pointed space is not opened dynamically , that free The behavior of a function is undefined
- If parameters ptr yes NULL The pointer , Then the function does nothing
Two 、free
When the dynamically requested space is no longer used , It should be returned to the operating system .
free(p);
p=NULL;//p When the pointer is set to null ,p You can't find that storage space
malloc And free Use in pairs , Otherwise, it will cause memory leakage
3、 ... and 、calloc
form : void* calloc(size_t num, size_t size)) Used for dynamic memory allocation
int *p=calloc(10,sizeof(int));
The function num Size is size The elements of open up a space , And initialize each byte of the space to 0;
And functions malloc The only difference is calloc It initializes each byte of the requested space to... Before returning the address 0.
Four 、realloc
form : void *realloc(void* ptr,size_t size); realloc Function can be used to adjust the size of dynamic development memory ;
ptr Is the memory address to be adjusted ,size Is the new size after adjustment , The return value is the starting memory position after adjustment , This function adjusts the size of the original memory space .

realloc Precautions for use :
1. If p There is enough space to add after the space pointed to , Then... Is added directly , After the return p;
2. If p There is not enough space to add after the space pointed to , be realloc The function will find a new memory area again , Open up a memory space to meet your needs , And copy the data in the original memory , Free up old memory space , Finally, return the newly opened memory space address ;
3. You have to accept... With a new variable realloc The return value of the function .
int main()
{
int *p=(int*)malloc(20);
if(p==NULL)
{
printf("%s\n",strerror(errno));
}
else
{
int i=o;
for(i=0;i<5;i++)
{
*(p+i)=i;
}
}
}
int *p2=realloc(p,40);
if(p2!=NULL) // Here we need to judge whether the newly opened space overflows
{
p=p2;
}
int i=0;
for(i=5;i<10;i++)
{
*(p2+i)=i;
}
for(i=0;i<10;i++)
{
printf("%d ",*(p2+i));
}
free(p);
p=NULL;
return 0;
}边栏推荐
- Basic usage of markdown (plain text and grammar)
- BM setup process
- How to crop GIF dynamic graph? Take this picture online clipping tool
- Iterator failure condition
- Design and implementation of thread pool
- Master XSS completely from 0 to 1
- ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析
- Position (5 ways)
- Using Sphinx to automatically generate API documents from py source files
- [try to hack] vulhub shooting range construction
猜你喜欢

QT loading third-party library basic operation

如何裁剪动图大小?试试这个在线照片裁剪工具

15 -- 最接近原点的 K 个点

How to view the Chrome browser plug-in location

ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析

2022年广东高考分数线出炉,一个几家欢喜几家愁

【Try to Hack】vulnhub DC1

Flexible layout (display:flex;) Attribute details

About the problem of kicad stuck in win10 version, version 6 x

SPARQL learning notes of query, an rrdf query language
随机推荐
Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
Character encoding minutes
JS recursion and while
Is it normal to dig for money? Is it safe to open a stock account?
Judging the number of leap years from 1 to N years
QT loading third-party library basic operation
[deep learning] multi label learning
网上办理股票开户安全吗?
Uniapp icon configuration
How to view the Chrome browser plug-in location
One question per day,
Disable scrolling in the iPhone web app- Disable scrolling in an iPhone web application?
JS Base64 Library Learning
Golang channel reading data
One question per day, a classic simulation question
弹性布局(display:flex;)属性详解
Differences between member variables and local variables
定位position(5种方式)
Thymeleaf Usage Summary
Iterator failure condition