C变量未初始化

C变量未初始化,c,ansi,C,Ansi,调试下面的代码片段时,我发现function=copy\u string(temp\u函数)不初始化变量函数(仍然指向0x0),即使在返回copycopy fromcopy_string()上指向包含正确结果的初始化内存地址 static char* copy_string(char* string) { char* copy = NULL; uint32_t length = 0U; length = strlen(string); copy = malloc

调试下面的代码片段时,我发现
function=copy\u string(temp\u函数)不初始化变量函数(仍然指向0x0),即使在
返回copy
copy from
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);
}
使用以下参数调用该函数:

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);
这种行为的原因是什么

编辑:
固定分配问题。

这是因为
分离测试中的
函数
参数是一个临时变量。因此它需要一个它指向的随机地址,因为在调用它之前,变量被初始化为NULL。Si我建议在调用函数或返回函数参数之前进行:
function=malloc(sizeof(function))

您正在设置
function
的值和
单独测试中的其他变量。但是,由于它们是通过值传递的,因此会更改调用函数中这些变量的值。

您需要为空终止符保留空间。这一行:

copy = malloc(sizeof(char) * length);
应该是:

copy = malloc(length + 1);
sizeof(char)
始终为1,因此这里不需要它

另外,请记住,C中的参数是按值传递的,因此调用方看不到您在
单独的测试()中对
测试名称
函数等所做的更改。您可能希望将指针传递给指针,如下所示:

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()
变成:

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);
}

strlen()是做什么的。@self你是什么意思?@IsKernel表示你没有用
malloc(sizeof(char)*length)
@self-True保留足够的内存,没有分配足够的内存。但这不是问题所在。@哎呀,我解决了,但这不是问题所在。感谢您的关注:-)@BLUEPIXY哈!抢手货我编辑了我的答案,谢谢你。