简体   繁体   English

C变量未初始化

[英]C variable not getting initialized

When debugging the following code snippet, I observed that function = copy_string(temp_function); 调试以下代码段时,我观察到该function = copy_string(temp_function); does not initialize the variable function (still points at 0x0), even though on return copy copy from copy_string() points to an initialized memory address containing the correct result. 不会初始化变量函数(仍指向0x0),即使在return copy副本时,来自copy_string()副本也指向包含正确结果的初始化内存地址。

static char* copy_string(char* string)
{
    char* copy = NULL;
    uint32_t length = 0U;

    length = strlen(string);
    copy = malloc(sizeof(char) * (length +1));
    strcpy(copy, string);

    return copy;
}

static void separate_test(const char* test_name, char* function, char* scenario, char* expected_result)
{
    const char* delimiter = "__";
    char* new_test_name = NULL;
    char* temp_function = NULL;
    char* temp_scenario = NULL;
    char* temp_expected_result = NULL;
    uint32_t length = strlen(test_name);

    new_test_name = malloc(sizeof(char) * (length +1));
    strcpy(new_test_name, test_name);
    temp_function = strtok(new_test_name, delimiter);
    function = copy_string(temp_function);
    temp_scenario = strtok(NULL, delimiter);
    scenario = copy_string(temp_scenario);
    temp_expected_result = strtok(NULL, delimiter);
    expected_result = copy_string(temp_expected_result);
}

The function is called with the following parameters: 该函数使用以下参数调用:

const char* test_name = "function_name__scenario__expected_result";
char* function = NULL;
char* scenario = NULL;
char* expected_result = NULL;

separate_test(test_name, function, scenario, expected_result);

What is the cause of this behavior? 这种现象的原因是什么?

Edit: Fixed allocation issue. 编辑:固定分配问题。

You are setting the value of function and other variables in separate_test . 您设置的值function在和其他变量separate_test However, since they are passed by value, that does change the values of those variables in the calling function. 但是,由于它们是按值传递的,因此确实会更改调用函数中这些变量的值。

You need to reserve space for the null terminator. 您需要为空终止符保留空间。 This line: 这行:

copy = malloc(sizeof(char) * length);

Should be: 应该:

copy = malloc(length + 1);

sizeof(char) is always 1, so you don't need it here. sizeof(char)始终为1,因此您在这里不需要它。

Also, recall that parameters in C are passed by value, so the changes you make to test_name , function , etc, inside separate_test() are not seen by the caller. 另外,请记住,C语言中的参数是按值传递的,因此调用者看不到您对test_name separate_test()中的test_namefunction等所做的更改。 You might want to pass pointers to pointers instead, like so: 您可能希望改为将指针传递给指针,如下所示:

const char* test_name = "function_name__scenario__expected_result";
char* function = NULL;
char* scenario = NULL;
char* expected_result = NULL;

separate_test(test_name, &function, &scenario, &expected_result);

separate_test() becomes: separate_test()变为:

static void separate_test(const char* test_name, char** function, char** scenario, char** expected_result)
{
    const char* delimiter = "__";
    char* new_test_name = NULL;
    char* temp_function = NULL;
    char* temp_scenario = NULL;
    char* temp_expected_result = NULL;
    uint32_t length = strlen(test_name);

    new_test_name = malloc(length+1);
    strcpy(new_test_name, test_name);
    temp_function = strtok(new_test_name, delimiter);
    *function = copy_string(temp_function);
    temp_scenario = strtok(NULL, delimiter);
    *scenario = copy_string(temp_scenario);
    temp_expected_result = strtok(NULL, delimiter);
    *expected_result = copy_string(temp_expected_result);
}

That is because the function parameter in seperate_test is a temporary variable. 这是因为seperate_testfunction参数是一个临时变量。 So it takes a random address to which it points to, since the before calling it the variable is initialized to NULL. 因此,它需要一个指向它的随机地址,因为在调用它之前,变量已初始化为NULL。 Si I would advise to make a : function = malloc(sizeof(function)) before calling the function or returning the function parameter. Si我建议在调用函数或返回函数参数之前先做一个: function = malloc(sizeof(function))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM