为什么相同的地址返回不同的值?

3
我正在尝试调试代码,据我理解,其等效于以下内容...
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
在这种情况下,“node”是什么? - Chris Gong
1
不是完全确定。我觉得通过调用function(node->ns),你只是传递了node->ns指针的一个副本。新指针将被放置在堆栈上,并获得它自己的地址。 - pstued
2
请使用"%p"而不是"%d"来打印指针。 - aschepler
4
因为我尝试重现您的程序并没有遇到任何问题,所以您需要提供更多信息。http://ideone.com/yop8px - Chris Gong
1
如果您自己解决了这个问题,应该回答您自己的问题,而不是更新问题。 - dbush
显示剩余3条评论
1个回答

1
您的问题最初的形式有两个问题:
  1. 代码不完整。
  2. 您正在使用(仍然是)指针的%d
因此,很难确定问题出在哪里。您之后的编辑,

修复版本,使用malloc代替

建议一个合理的可能性:您正在使用超出其范围的自动数组。在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具有所谓的自动存储。 (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返回,自动变量line_numbers就会超出范围。编译器在那时可以自由地重新使用已分配给该变量的存储空间。是的,您仍然有它在node中的地址,但编译器很快就会用这个空间来做其他事情。当您打印该地址处的值时,您可能会发现与您放置的内容不同。

完美,我现在明白了。我没有发布所有的代码,因为程序中有很多无用的内容,我认为这些内容并不需要展示问题。谢谢! - Pig

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接