0

我创建了一个非常简单的链表,并注意到我的代码的tcc filename.cvs输出有所不同:tcc 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然后运行它会产生我期望的输出:

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

最后一个数字始终为零,而其他两个额外的值每次运行时都不同。

我想出了l1 -> next = NULL;newList函数和函数l2 -> next = NULL;中添加的解决方案appendList

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

4

3 回答 3

1

我想出了l1 -> next = NULL;newList函数和函数l2 -> next = NULL;中添加的解决方案appendList

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

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

此外,您可能会发现您的原始方法无法与您在更复杂情况下测试的其他编译器一起可靠地工作(但我只能对此做出概率性陈述,因为“未定义”)。

于 2017-07-07T17:54:56.797 回答
0

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

初始化下一个变量。

于 2017-07-07T17:52:39.967 回答
0

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

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

于 2017-07-07T19:26:45.930 回答