1. The requested memory is released multiple times
int main(void){
int *p = new int[10];
p[0] = 100;
printf("p[0]'s value: %d\n",*p);
delete[] p; //释放内存
delete[] p;
return 0;
}
2. Memory leak-memory has not been released after application
int main(void){
int *p = new int[1024];
p[0] = 100;
printf("p[0]'s value: %d\n",*p);
while(1){
*p++;
}
system("pause");
return 0;
}
3. The released memory is not the address at the time of application
int main(void){
int *p = new int[1024];
p[0] = 100;
printf("p[0]'s value: %d\n",*p);
for(int i=0; i<10; i++){
*p++;
}
delete[] p; //释放内存
return 0;
}
4. Release the null pointer.
There may be an error, but you can’t write like this if you don’t report an error.
int main(void){
int *p = NULL;
if(1==0){
p = new int;
}
delete p; //释放内存
return 0;
}
5. Release a memory block, but continue to reference its contents
int main(void){
int *p = new int[10];
delete p; //释放内存
p[0] = 100;
printf("p[0]'s value: %d\n",*p);
return 0;
}
6. Cross-border access
int main(void){
// 越界访问
int *p = new int[10];
memset(p,0,18*sizeof(int));
for(int i=0; i<10; i++){
printf("*p's value: %d\n",*p++);
}
for(int i=0; i<10; i++){
printf("*p's value: %d\n",*p++);
}
return 0;
}