在C

时间:2015-07-29 09:18:01

标签: c memory memory-management dynamic structure

我有以下结构:

typedef struct generic_attribute_struct{
    attribute_value current_value;
    attribute_value previous_value;
    attribute_value running_value;
} generic_attribute;

其中attribute_value只是unsigned long long int的占位符。

我有这个结构的以下构造函数:

generic_attribute* construct_generic_attribute(attribute_value current_value){
    generic_attribute *ga_ptr;                              //allocate space for pointer to generic attribute
    if(ga_ptr = malloc (sizeof (generic_attribute))){       //if allocation succeeds
        set_ga_current_value(ga_ptr, current_value);        //assigned the current value to given input
        set_ga_previous_value(ga_ptr, 0);                   //set previous value to zero
        set_ga_running_value(ga_ptr);
    } else{                                                 //else, inform user of error
        fprintf(stderr, "Health Monitor ran out of space when allocating memory for a generic attribute.");
    }
    return ga_ptr; // return pointer to new attribute or a NULL pointer if memory allocation failed
}

set_ga_running_value看起来像这样:

attribute_value set_ga_running_value(generic_attribute* ga_ptr){
    attribute_value delta = get_ga_current_value(ga_ptr) - get_ga_previous_value(ga_ptr);
    ga_ptr->running_value = (ga_ptr->running_value? ga_ptr->running_value : 0) + delta;
    return ga_ptr->running_value;
}

此结构的析构函数如下所示:

void destroy_generic_attribute(generic_attribute** ga_ptr){
    free(*ga_ptr);
}

我创建了一个测试,要求用户输入一些current_value,构造一个指针,并打印出结构变量的值是否应该是它们应该是什么。此测试循环直到用户不再需要测试,在这种情况下,他们退出测试。

所以,测试看起来像这样:

  1. 用户想要测试吗?如果是,请转到2)。如果不是,请转到7)。
  2. 从用户那里获取输入
  3. 使用此新输入调用通用属性的构造函数
  4. 验证是否正确创建了通用属性
  5. 在通用属性
  6. 上调用析构函数
  7. 转到1.
  8. 退出
  9. 这就是测试的样子:

    void test_generic_attribute_constructor_with_user_input(){
        attribute_value input;
        int continue_var;
        bool current_value_test, previous_value_test, running_value_test, pointer_test;
        generic_attribute* ga_ptr;
        while(1){
            printf("Would you like to execute a test for Generic Attribute Constructor? Type 1 for YES, or 0 for NO: ");
            scanf("%i", &continue_var);
            if(continue_var){
                printf(TESTING, "Constructor for Generic Attribute and Generic Attribute Reader");
                printf("\n" INPUT, "single number");
                scanf("%lld", &input);
                ga_ptr = construct_generic_attribute(input);
                read_generic_attribute(ga_ptr);
                current_value_test = (get_ga_current_value(ga_ptr) == input ? true : false);
                previous_value_test = (get_ga_previous_value(ga_ptr) == 0 ? true: false);
                // THIS TEST FAILS
                running_value_test = (get_ga_running_value(ga_ptr) == input ? true: false);
                pointer_test = (ga_ptr ? true: false);
                printf("%s.\n", ((current_value_test && previous_value_test && running_value_test && pointer_test) ? PASS : FAIL));
                destroy_generic_attribute(&ga_ptr);
    
            }else{
                printf("\nOK! Testing concluded.");
                break;
            }
    
        }
    }
    

    我的问题是"运行价值"好像永远不会被重置"当ga_ptr被摧毁时它似乎保持其旧的价值。如何正确清除整个ga_ptr结构的内存?

    测试结果:

    Would you like to execute a test for Generic Attribute Constructor? Type 1 for YES, or 0 for NO: 1
    Testing Constructor for Generic Attribute and Generic Attribute Reader:
    Please enter single number for input: 10
    Generic Attribute has the following contents:
    
                 Pointer       Current Value       Previos Value       Running Value
             0x600058530                  10                   0                  10
    PASS.
    Would you like to execute a test for Generic Attribute Constructor? Type 1 for YES, or 0 for NO: 1
    Testing Constructor for Generic Attribute and Generic Attribute Reader:
    Please enter single number for input: 20
    Generic Attribute has the following contents:
    
                 Pointer       Current Value       Previos Value       Running Value
             0x600058530                  20                   0                  30
    FAIL.
    

    我希望Running值为20。

    如果我将析构函数更改为:

    void destroy_generic_attribute(generic_attribute** ga_ptr){
        set_ga_current_value(*ga_ptr, 0);
        set_ga_previous_value(*ga_ptr, 0);
        (*ga_ptr)->running_value = 0;
        free(*ga_ptr);
    }
    

    我的测试通过......但是,我不明白为什么跳过setter会导致代码失败。

1 个答案:

答案 0 :(得分:1)

您只是使用从未初始化的值来调用未定义的行为。

construct_generic_attribute中,您初始化当前值和上一个值,然后调用set_ga_running_value。在后者中,您使用刚刚初始化的当前值和先前值来计算delta:罚款直到此处。但是你有:

ga_ptr->running_value = (ga_ptr->running_value? ga_ptr->running_value : 0) + delta;

表示在初始化之前使用running_value。正如它在一个新的malloc结构中,它的值只是 undefined 。它可能是0,或者它可能是分配前在此内存位置中存在的值,或者它可能是编译器用作特殊初始化的特殊值:您无法知道并且依赖于任何东西。

你的编译器似乎在运行时将内存预先初始化为0,然后永远不会更改free和malloc上的值,在第二次运行时给出30。我的(在调试模式下)总是将malloc的值初始化为0xcdcdcdcd,每次测试都给出FAIL。只是未定义的行为......

所以你真的应该在构造函数中初始化已刷新的已分配结构:

if(ga_ptr = malloc (sizeof (generic_attribute))){       //if allocation succeeds
    memset(ga_ptr, 0, sizeof(generic_attribute));       // ensure all values are set to 0
    set_ga_current_value(ga_ptr, current_value);        //assigned the current value to given input
    set_ga_previous_value(ga_ptr, 0);                   //set previous value to zero
    set_ga_running_value(ga_ptr);

或者如果您知道running_value是一个整数,只需将memset替换为:

ga_ptr->running_value = 0;