c - 为什么同一个地址返回不同的值?

标签 c pointers

我正在尝试调试代码 - 根据我的理解 - 等同于以下...

printf("outside function: %d %d\n", node->ns, node->ns[0]);
function(node->ns);
void function(int *ns) {
     printf("inside function: %d %d\n", ns, ns[0]);
}

输出:

outside function: 11532860 1
inside function: 11532860 11532872

这是预期的,还是我对代码的解释不正确?那个“11532872”有点可疑。

更新

我解决了!问题在于节点结构的创建。

原始版本,其中 function()(来自上面的代码片段)== _contains():

typedef struct TreeNode {
    char *word;
    int lines;
    int *line_numbers;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

TreeNode *allocate_tree_node(void) {
    return (TreeNode *) malloc(sizeof(TreeNode));
}

TreeNode *create_tree_node(char *word, int line) {
    int line_numbers[MAX_LINES_PER_WORD]; // <--- the problem
    TreeNode *node = allocate_tree_node();
    node->line_numbers = line_numbers; // <--- the problem
    *node->line_numbers = line;
    node->lines = 1;
    node->word = duplicate_string(word);
    node->left = node->right = NULL;
    return node;
}

int _contains(int *ns, int n, int size) {
    int i;
    printf("contains: %d %d %d\n", ns, ns[0], *ns);
    for (i = 0; i < size; i++) {
        printf("line: %d, index: %d, val: %d\n", n, i, ns[i]);
        if (ns[i] == n) {
            return 1;
        }
    }
    return 0;
}

TreeNode *add_tree(TreeNode *node, char *word, int line) {
    int comparison;

    if (node == NULL) {
        return create_tree_node(word, line);
    }

    comparison = strcmp(word, node->word);
    if (comparison == 0 && !_contains(node->line_numbers, line, node->lines)) {
        printf("not contains: %d %d\n", node->line_numbers, node->line_numbers[0]);
        node->line_numbers[node->lines] = line;
        node->lines++;
    }
    else if (comparison < 0) {
        node->left = add_tree(node->left, word, line);
    }
    else {
        node->right = add_tree(node->right, word, line);
    }

    return node;
}

固定版本,使用 malloc 代替数组赋值:

TreeNode *create_tree_node(char *word, int line) {
    TreeNode *node = allocate_tree_node();
    node->line_numbers = malloc(MAX_LINES_PER_WORD * (sizeof node->line_numbers));
    *node->line_numbers = line;
    node->lines = 1;
    node->word = duplicate_string(word);
    node->left = node->right = NULL;
    return node;
}

输出现在是正确的。有人介意解释一下发生了什么吗?

(代码基于 K&R 第 6 章,练习 3)。

最佳答案

您问题的原始形式有两个问题:

  1. 代码不完整。
  2. 您曾经(现在仍然)使用 %d 作为指针。

正因为如此,很难确定哪里出了问题。您的后续编辑,

Fixed version, using malloc instead

提出了一种合理的可能性:您在其范围之外使用了一个自动数组。这在 C 语言中称为未定义行为,如果确实如此,那么奇怪的值就不足为奇了。

进一步的证据在这里:

TreeNode *create_tree_node(char *word, int line) {
    int line_numbers[MAX_LINES_PER_WORD]; // <--- the problem
    TreeNode *node = allocate_tree_node();
    node->line_numbers = line_numbers; // <--- the problem
    *node->line_numbers = line;
    node->lines = 1;
    node->word = duplicate_string(word);
    node->left = node->right = NULL;
    return node;
}

node->line_numbers 是一个指针,局部变量 line_numbers 具有所谓的 automatic 存储。 (C 程序员经常将自动变量称为“在堆栈上”,有时确实如此,但“堆栈”不是 C 概念。)与所有变量一样,自动变量仅在其作用域内定义。如果你获取一个地址,并尝试在变量范围之外使用该地址,行为是未定义的,而且几乎总是不是你想要的。

当你这样做时,这就是你介入的汤:

int line_numbers[MAX_LINES_PER_WORD];
node->line_numbers = line_numbers;
...
return node;

如您所知,在 C 语言中,数组的名称就是它的地址。您的数组是 line_numbers。您将其地址分配给一个指针,node->line_numbers
一旦 create_tree_node 返回,create_tree_node 的本地自动变量,即 line_numbers 超出范围。此时的编译器可以自由地重新使用它分配给变量的存储空间。是的,您仍然在 node 中拥有它的地址,但编译器迟早会将该空间用于其他用途。果然,当你打印出那个地址的值时,你很可能会发现你放在那里的东西以外的东西。

关于c - 为什么同一个地址返回不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927264/

相关文章:

c - 如何消除所有也出现在ch2中的字母ch1(c语言)

c - 为什么c脚本只在最后一次执行循环时重命名

c - 指针给出了意想不到的值(value)

C:sha256 哈希函数在结构体数组的字段中输出,导致将结构体内容写入磁盘时崩溃

c++ - 使用 std::atomic 中的方法时是否隐式调用 load()?

C++模板使用模板内类的指针的不完整类型

交叉编译、Crosstool、Makefile、libreadline

c++ - 多个类对象共享公共(public)变量

c - C语言中如何从指针返回值

c - 按 C 顺序查找带元音的单词