当前位置:网站首页>Common dynamic memory errors
Common dynamic memory errors
2022-06-25 14:58:00 【--988】
- Yes NULL Dereference operation of pointer
void test()
{
int *p=(int*)malloc(INT_MAX/4);
*p=20; // If p The value of is NULL, There will be problems
free(p);//malloc Failed to open up space —— Yes NULL Pointer dereference
}
- Opening up space for dynamic memory out of bounds access
void test()
{
int i=0;
int *p=(int*)malloc(10*sizeof(int));
if(NULL==p)
{
exit(EXIT_FAILURE);
}
for(i=0;i<=10;i++) // When i=10 when , A cross-border visit
{
*(p+i)=i;
}
free(p);
}
- Use of non dynamic memory free Release
{
int a=10;
int *p=&a;
free(p);//error
}
- Use free Release a piece of dynamic memory
{
int *p=(int*)malloc(100);
p++;
free(p);//p No longer points to the start of dynamic memory
}

*int p=realloc(NULL,40)<=>malloc(40)
- Multiple releases of the same dynamic memory
{
int *p=(int*)malloc(100);
free(p);
//p=NULL; // if p=NULL, hinder free It is meaningless
free(p); // Repeat release
}
- Dynamic memory forget to release ( Memory leak )
#include<windows.h>
while(1)
{
malloc(1);
}
return 0;
}
ask : function Test What is the result of the function ?
void GetMemory(char*p) // p yes str A temporary copy of
{
p=(char*)malloc(100);
}
void Test(void)
{
char *str=NULL;
GetMemory(str); // Whether it's pointer variables or other variables , Just value transfer
strcpy(str,"hello");
printf(str);
}
int main()
{
Test();
return 0;
}
answer :1. The running code program crashes
2. There is a memory leak in the program
analysis :
str Pass a value to p,p yes GetMemory The formal parameter of the function , Can only be valid inside a function , etc. GetMEmory Function returns a value , Dynamic memory development has not been freed and cannot be found , So it causes a memory leak .
correct
void GetMemory(char **p)
{
*p=(char*)malloc(100);// Quoting
}
void Test(void)
{
char *str=NULL;
GetMemory(&str);
strcpy(str,"hello");
printf(str);
free(str);
str=NULL;
}

correct 2
char* GetMemory(char*p)
{
p=(char*)malloc(100);
return p;// When the function comes out , return p, use str receive p Address
}
void Test(void)
{
char *str=NULL;
str=GetMemory(str);
strcpy(str,"hello");
printf(str);
free(str);
str=NULL;
}
int main()
{
Test();
return 0;
}
Here is the stack area , Heap area , Static area return address problem
char* GeMemory(void)
{
char p[]="hello";// local variable
return p;
}
void Test(void)
{
char *str=NULL;
str=GetMemory();// Illegal access to memory space
printf(str);
}
int mian()
{
Test();
return 0;
}
int *test()
{
int *ptr=malloc(100);
return ptr;
}
int main()
{
int *p=test();
return 0;
}// Stack area not free The address will not be destroyed
int *test()
{
static int a=10; // Static area data will not be lost
return &a;
}
int mian()
{
int *p=test();
*p=20;// modify a The value of is 20
return 0;
}
complete
int f2(void)—— Wild pointer
{
intptr;——ptr Not initialized , It's a random value , Dereference will result in illegal access to memory
*ptr=10;
return ptr;
}
Run the following Test What is the result of the function ?
void Test(void)
{
char *str=(char*)malloc(100);
strcpy(str,"hello");
free(str);//free Release str After pointing to the space , It doesn't mean that str Set as NULL
if(str!-NULL)// Judgment makes sense
{
strcpy(str,"World");
printf(str);
}
}
int main()
{
Test();
return 0;
}
answer : Tampering with the contents of dynamic memory , The consequences are unpredictable , Very dangerous ;
because free(str) after ,str Become a wild pointer ,if(str!=NULL) Sentences don't work .
C/C++ Several areas of program memory allocation
- The stack area : When executing a function , The storage units of function local variables can be created on the stack , At the end of the function, these storage units are automatically released . Stack memory allocation operations are built into the processor's instruction set , It's very efficient , But the allocated memory capacity is limited . The stack area mainly stores the local variables allocated by running functions , Function parameter , Return the data , Return address, etc .
- Heap area ; Release is usually assigned by the programmer , If programmers don't release , At the end of the program, it may be 0S Operating system recycling . The distribution is similar to a linked list
- Data segment : The static area stores global variables , Static data . Released by the system at the end of the program
- Code segment : Store function body ( Class member functions and global functions ) The binary code of
static Keywords modify local variables
Common local variables are really stack allocated space , The characteristic of stack area is that the variables created above are destroyed when they are out of scope ;
But being static The modified variables are stored in the data section , Data segments are characterized by variables created above , Not until the end of the program , So the life cycle gets longer .
边栏推荐
- 网上股票开户安不安全?有谁知道呢
- Go closure usage example
- JS Base64 Library Learning
- SPARQL learning notes of query, an rrdf query language
- JGG | overview of duhuilong group of Hebei University Research on plant pan genomics
- Function of getinstance() method
- 重磅!国产 IDE 发布,由阿里研发,完全开源!(高性能+高定制性)
- Qlogsystem log system configuration use
- Usage of qlist
- 网上办理股票开户安全吗?
猜你喜欢

Kubernetes 理解kubectl/调试

Learning notes on February 18, 2022 (C language)

Character encoding minutes

Yolov3 spp Darknet version to caffemodel and then to OM model

Iterator failure condition

Flexible layout (display:flex;) Attribute details

关于win10 版本kicad 卡死的问题, 版本6.x

Learning notes on February 5, 2022 (C language)

Jaspersoft studio installation

About the problem of kicad stuck in win10 version, version 6 x
随机推荐
JGG | overview of duhuilong group of Hebei University Research on plant pan genomics
全国首例,中国电信 5G 井下人员定位项目正式商用:可实时跟踪位置,保障作业安全
Clinical chemistry | zhangjianzhong / Xu Jian develop single cell precision diagnosis and treatment technology for Helicobacter pylori
What is the safest app for stock account opening? Tell me what you know
[HBZ sharing] use of locksupport
Brain tree (I)
Extend JS copy content to clipboard
Common classes in QT
Mutationobserver listens for DOM changes
【深度学习】多任务学习 多个数据集 数据集漏标
@Font face fonts only work on their own domain - @font-face fonts only work on their own domain
QT pop up open file dialog box QFileDialog
搭建极简GB28181 网守和网关服务器,建立AI推理和3d服务场景,然后开源代码(一)
How to make GIF animation online? Try this GIF online production tool
现在股票开户用什么app最安全?知道的给说一下吧
Luogu p5707 [deep foundation 2. example 12] late for school
挖财是正规的吗?股票开户安全吗?
Uniapp icon configuration
For the first time in China, Chinatelecom 5g underground personnel positioning project is officially commercial: it can track the position in real time to ensure operation safety
90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟