小编Gop*_*hna的帖子

为什么下面的代码没有给出双重自由错误?

当我free()在同一个指针上连续执行两次时,它会产生双重自由错误,但是当我尝试释放两次相同的指针时,我释放了其他指针,它没有给出错误.

#include <stdio.h>                                                              
#include <stdlib.h>                                                             
int main (){                                                                               
   long int*ptr;                                                              
    int *ptr1;                                                                  

    ptr = malloc (1);                                                           
    ptr1 = malloc (1);                                                          

    printf ("%ld\n", ptr[-1]);                                                  
    free (ptr);                                                                 
    printf ("%ld\n", ptr[-1]);                                                  
    free (ptr1);                                                                
    free (ptr);                                                                 
    free (ptr1);                                                                
    free (ptr);                                                                 
    free (ptr1);                                                                
    return 0;                                                                   
} 
Run Code Online (Sandbox Code Playgroud)

c undefined-behavior

2
推荐指数
1
解决办法
109
查看次数

标签 统计

c ×1

undefined-behavior ×1