首页 > 解决方案 > GCC 是否具有检测 use-after-free 错误的功能?

问题描述

这是我的代码片段:

#include <stdlib.h>
#include <stdio.h>

typedef struct node
{
    char key;          // value
    struct node *next; // pointer to the next element
} Node;

int main(void)
{
    Node *n = malloc(sizeof(Node));
    n->key = 'K';
    n->next = NULL;

    free(n);

    printf("%c\n", n->key);
}

当上面的代码片段被编译并运行时......

ganningxu@Gannings-Computer:~/test$ gcc test_faults.c -o test_faults; ./test_faults
K

ganningxu@Gannings-Computer:~/test$ clang test_faults.c -o test_faults; ./test_faults
K

访问释放的内存时没有编译器警告或错误。有没有办法强制编译器显示此类错误?

标签: cgccclang

解决方案



推荐阅读