二进制的分割故障

发布于 2025-02-05 14:09:43 字数 2246 浏览 2 评论 0原文

我正在研究一个程序,该程序通过程序进行搜索,并输出它的数量大于或等于函数参数中指定的值。我已将代码附加在下面,我看到它在发生分割故障之前遍历了两个值。分段故障错误指向返回count + count_values(value,ever) + count_values(value,current);>

typedef struct BinaryTree {
    int val;
    struct BinaryTree *left;
    struct BinaryTree *right;
} BinaryTree;

BinaryTree *build_tree(int value, BinaryTree *leftnode, BinaryTree *rightnode) {
    BinaryTree *out = calloc(1, sizeof(BinaryTree));
    out->val = value;
    out->leftnode = leftnode;
    out->rightnode = rightnode;

    return out;
}

int count_values(int value, BinaryTree *tree) {
  
    BinaryTree *current = tree;
    BinaryTree *here = current;
    int count = 0;

    if (current != NULL) {
        printf("Value in tree is not NULL\n");

        if (current->val < value) {
            printf("%d < %d\n", current->val, value);
            printf("Count value: %d\n", count);
            here = current->leftnode;
            count_values(value, here);
            current = current->rightnode;
            count_values(value, current);

        } else if (current->val == value) {
            printf("%d = %d\n", current->val, value);
            count++;
            printf("Count value: %d\n", count);
            here = current->leftnode;
            count_values(value, here);
            current = current->rightnode;
            count_values(value, current);
        } else {
            printf("%d > %d\n", current->val, value);
            count++;
            printf("Count value: %d\n", count);
            here = current->leftnode;
            count_values(value, here);
            current = current->rightnode;
            count_values(value, current);
        }
    }
        
    return count + count_values(value, here) + count_values(value, current);
}

int main(void) {
    BinaryTree *tree =
        build_tree(14, build_tree(3, NULL, NULL),
                   build_tree(15, NULL, build_tree(42, NULL, NULL)));

    //path is 14->15->42
    int count = count_values(42, tree);
    printf("should have a count of 1, got %d\n", count);

    count = count_values(14, tree);
    printf("should have a count of 3, got %d\n", count);

    return 0;
}

I'm working on a program that searches through a program and outputs the number of times it comes across a number greater than or equal to the value specified in the parameter of the function. I have attached my code below, I see it traverses two of the values before a segmentation fault occurs. The segmentation fault error points to return count + count_values(value, here) + count_values(value, current);

typedef struct BinaryTree {
    int val;
    struct BinaryTree *left;
    struct BinaryTree *right;
} BinaryTree;

BinaryTree *build_tree(int value, BinaryTree *leftnode, BinaryTree *rightnode) {
    BinaryTree *out = calloc(1, sizeof(BinaryTree));
    out->val = value;
    out->leftnode = leftnode;
    out->rightnode = rightnode;

    return out;
}

int count_values(int value, BinaryTree *tree) {
  
    BinaryTree *current = tree;
    BinaryTree *here = current;
    int count = 0;

    if (current != NULL) {
        printf("Value in tree is not NULL\n");

        if (current->val < value) {
            printf("%d < %d\n", current->val, value);
            printf("Count value: %d\n", count);
            here = current->leftnode;
            count_values(value, here);
            current = current->rightnode;
            count_values(value, current);

        } else if (current->val == value) {
            printf("%d = %d\n", current->val, value);
            count++;
            printf("Count value: %d\n", count);
            here = current->leftnode;
            count_values(value, here);
            current = current->rightnode;
            count_values(value, current);
        } else {
            printf("%d > %d\n", current->val, value);
            count++;
            printf("Count value: %d\n", count);
            here = current->leftnode;
            count_values(value, here);
            current = current->rightnode;
            count_values(value, current);
        }
    }
        
    return count + count_values(value, here) + count_values(value, current);
}

int main(void) {
    BinaryTree *tree =
        build_tree(14, build_tree(3, NULL, NULL),
                   build_tree(15, NULL, build_tree(42, NULL, NULL)));

    //path is 14->15->42
    int count = count_values(42, tree);
    printf("should have a count of 1, got %d\n", count);

    count = count_values(14, tree);
    printf("should have a count of 3, got %d\n", count);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2025-02-12 14:09:44
  1. 中,如果(current-&gt; val!= null){ current-&gt; value永远不可能为null指针,因为值不是指针。只是int。您可能应该检查当前,而不是current-&gt; val
  2. 即使您更正了该行,您的程序可能会导致堆叠溢出(hehe),因为您无条件地从本身在返回count + count_values(value,there) + count_values(value,current); line

P.S 中您发布的代码需要进行一些调整才能构建。如果可以的话,请修复

  1. In if (current->val != NULL) { current->value can never be NULL pointer as value isn't pointer. It is just int. You probably should check current instead of current->val
  2. Even if you corrected that line your's program would segfault cause to stack overflow (hehe) because you unconditionally call your function from itself in return count + count_values(value, here) + count_values(value, current); line

P.S. the code you posted needed some adjustments to build. Fix it if you can

渔村楼浪 2025-02-12 14:09:44

对于初学者,这两个函数中都有错别字,例如

out->leftnode = leftnode;
out->rightnode = rightnode;

here = current->leftnode;
current = current->rightnode;

您必须编写

out->left = leftnode;
out->right = rightnode;

here = current->left;
current = current->right;

Count_values 中的函数在此If语句

if (current->val != NULL) {

调用不确定的行为。

首先,这是没有意义的。其次,在访问节点的数据成员之前,您需要检查指针当前(更准确地说,指针tree)是否是空指针。

注意这样的陈述

count_values(value, here);
count_values(value, current);

实际上没有影响。

可以按照以下方式定义递归函数,

size_t count_values( const BinaryTree *tree, int value ) 
{
    return tree == NULL 
        ? 0 
        : ( tree->val >= value ) + count_values( tree->left, value ) + count_values( tree->right, value );
}    

将树指针应为第一个函数参数,并使用限定器const声明,因为该函数不会更改树。并且该函数应返回未签名整数类型的对象,因为计数器不能为负值。代替返回类型size_t您可以使用例如类型无符号int

这是一个演示程序。

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

typedef struct BinaryTree {
    int val;
    struct BinaryTree *left;
    struct BinaryTree *right;
} BinaryTree;

BinaryTree *build_tree(int value, BinaryTree *leftnode, BinaryTree *rightnode) {
    BinaryTree *out = calloc(1, sizeof(BinaryTree));
    out->val = value;
    out->left = leftnode;
    out->right = rightnode;

    return out;
}

size_t count_values( const BinaryTree *tree, int value ) 
{
    return tree == NULL 
        ? 0 
        : ( tree->val >= value ) + count_values( tree->left, value ) + count_values( tree->right, value );
} 

int main(void) {
    BinaryTree *tree =
        build_tree(14, build_tree(3, NULL, NULL),
                   build_tree(15, NULL, build_tree(42, NULL, NULL)));

    //path is 14->15->42
    size_t count = count_values( tree, 42);
    printf("should have a count of 1, got %zu\n", count);

    count = count_values(tree, 14 );
    printf("should have a count of 3, got %zu\n", count);

    return 0;
}

它的输出是

should have a count of 1, got 1
should have a count of 3, got 3

For starters there are typos in the both functions as for example

out->leftnode = leftnode;
out->rightnode = rightnode;

and

here = current->leftnode;
current = current->rightnode;

You have to write

out->left = leftnode;
out->right = rightnode;

and

here = current->left;
current = current->right;

Within the function count_values already this if statement

if (current->val != NULL) {

invokes undefined behavior.

First of all it does not make a sense. And secondly before accessing data members of a node you need to check whether the pointer current (more precisely the pointer tree) is a null pointer or not.

Pay attention to that such statements like these

count_values(value, here);
count_values(value, current);

in fact have no effect.

The recursive function could be defined the following way

size_t count_values( const BinaryTree *tree, int value ) 
{
    return tree == NULL 
        ? 0 
        : ( tree->val >= value ) + count_values( tree->left, value ) + count_values( tree->right, value );
}    

The pointer to the tree should be the first function parameter and declared with the qualifier const because the function does not change the tree. And the function should return an object of an unsigned integer type because the counter can not be a negative value. Instead of the return type size_t you may use for example the type unsigned int.

Here is a demonstration program.

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

typedef struct BinaryTree {
    int val;
    struct BinaryTree *left;
    struct BinaryTree *right;
} BinaryTree;

BinaryTree *build_tree(int value, BinaryTree *leftnode, BinaryTree *rightnode) {
    BinaryTree *out = calloc(1, sizeof(BinaryTree));
    out->val = value;
    out->left = leftnode;
    out->right = rightnode;

    return out;
}

size_t count_values( const BinaryTree *tree, int value ) 
{
    return tree == NULL 
        ? 0 
        : ( tree->val >= value ) + count_values( tree->left, value ) + count_values( tree->right, value );
} 

int main(void) {
    BinaryTree *tree =
        build_tree(14, build_tree(3, NULL, NULL),
                   build_tree(15, NULL, build_tree(42, NULL, NULL)));

    //path is 14->15->42
    size_t count = count_values( tree, 42);
    printf("should have a count of 1, got %zu\n", count);

    count = count_values(tree, 14 );
    printf("should have a count of 3, got %zu\n", count);

    return 0;
}

Its output is

should have a count of 1, got 1
should have a count of 3, got 3
顾忌 2025-02-12 14:09:44

您使这种方式比需要更难。

通过测试treenull,首先测试基本情况。

如果不是,请在左和右节点上重复,并将这些计数添加到当前节点的计数中。

int count_values(int value, BinaryTree *tree) {
    if (tree == NULL) {
        return 0;
    }

    int count = tree->val >= value;
    count += count_values(value, tree->left);
    count += count_values(value, tree->right);

    return count;
}

You're making this way harder than it needs to be.

First test for the base case by testing whether tree is NULL.

If not, recurse on the left and right nodes, and add these counts to the count for the current node.

int count_values(int value, BinaryTree *tree) {
    if (tree == NULL) {
        return 0;
    }

    int count = tree->val >= value;
    count += count_values(value, tree->left);
    count += count_values(value, tree->right);

    return count;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文