tcc中的空指针检测

Null pointer detection in tcc

我创建了一个非常简单的链表,并注意到 tcc filename.ctcc filename.c -run 的代码输出不同:

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

struct llist {
    struct llist *next;
    struct llist *last;
    struct llist *first;
    int value;
    int item;
    int *length;
};

struct llist *newList(int v){

    struct llist *l1 = malloc(sizeof(struct llist)); 
    l1 -> length = malloc(sizeof(int));
    *l1 -> length = 1;

    l1 -> value = v;    
    l1 -> item = 0;    
    l1 -> first = l1;

    return l1;
}

struct llist *appendList(struct llist *l1, int v){

    struct llist *l2 = malloc(sizeof(struct llist));

    l2 -> value = v;
    l2 -> last = l1;
    l2 -> first = l1 -> first;
    l2 -> length = l1 -> length; 
    *l2 -> length += 1; 
    l2 -> item = l1 -> item + 1;

    l1 -> next = l2;

    return l2;    
};

int main(){
    struct llist *list = newList(4);
    list = appendList(list, 6);
    list = appendList(list, 8);
    list = appendList(list, 10);

    list = list -> first;

    int end = 0;
    while(end==0){

        printf("VAL: %d\n", list -> value);

        if(list -> next == NULL){
            printf("END\n");
            end = 1;
        }else{

            list = list -> next;
        }
    }


    return 0;
}

对于使用 tcc filename.c 进行编译,然后 运行ning 它会产生我预期的输出:

VAL: 4
VAL: 6
VAL: 8
VAL: 10
END

这也是我在 GCC 和 clang 中得到的输出。

当我使用 tcc filename.c -run 时,我得到:

VAL: 4
VAL: 6
VAL: 8
VAL: 10
VAL: 27092544
VAL: 1489483720
VAL: 0
END

每次我 运行.

最后一个数字总是零,另外两个额外的值都不一样

我找到了在 newList 函数中添加 l1 -> next = NULL; 并在 appendList 函数中添加 l2 -> next = NULL; 的解决方案。

但我想知道为什么输出会有所不同。编译器中是否存在错误,或者我没有初始化指向 NULL 的指针是错误的,即使它在大多数编译器中都有效?

通常当您调试程序时,调试器会初始化所有内容,但在生产中没有初始化,因此下一个值不为空

初始化下一个变量。

I figured out the solution which was adding l1 -> next = NULL; in the newList function and l2 -> next = NULL; in the appendList function.

But I was wondering why there was a difference in output. Is there a bug in the compiler or was I wrong for not initialising the pointer to NULL even though it works in most compilers?

你在没有给它赋值或导致它被显式或隐式初始化(​​这与赋值不同)的情况下访问指针的值是错误的。这样做会产生未定义的行为。该程序在某些情况下仍然碰巧表现出您预期的行为是一个可能且合理的结果,但它并未验证该程序。

此外,您可能会发现您的原始方法不能与您在更复杂的情况下测试的其他编译器一起可靠地工作(但我只能做一个关于概率的陈述那,因为 "undefined").

函数 calloc() returns 指向初始化为零的字节序列的指针;相比之下,malloc() returns 指向字节序列的指针,该字节序列最初可能包含也可能不包含零。在某些平台上,这些字节在 malloc() 之后将始终包含零;在其他人身上,至少其中一些永远不会。通常,无法预测哪些字节为零,哪些字节不为零。

在大多数平台上,包括过去几十年创建的几乎所有平台,清除指针对象的所有字节会将指针设置为 NULL。在记录此类行为的平台上,使用 "calloc" 而不是 "malloc" 为包含指针的结构创建 space 将是将其中所有指针初始化为 NULL 的可靠方法。但是,如果使用 "malloc" 或 "realloc" 而不是 "calloc" 创建存储,则有必要使用 "memset" 将所有字节设置为零,或者明确设置其中包含指向 NULL 的指针。