2014-05-24 2 views
-1

다음 코드 조각을 디버깅 할 때 return copy 복사본에서 copy_string()의 복사본이 올바른 결과를 포함하는 초기화 된 메모리 주소를 가리킬지라도 function = copy_string(temp_function);은 변수 함수 (여전히 0x0을 가리킴)를 초기화하지 않는다는 것을 관찰했습니다.C 변수가 초기화되지 않음

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

이 문제의 원인은 무엇입니까?

편집 : 고정 할당 문제.

+1

()는 않습니다. – this

+0

@self 그게 무슨 소리 야? – IsKernel

+0

@malloc (sizeof (char) * length)로 충분한 메모리를 예약하지 않은 @IsKernel – ouah

답변

1

null 종결 자의 공간을 예약해야합니다. 이 줄은 :

copy = malloc(length + 1); 

sizeof(char) 항상 1, 그래서 당신은 여기 필요가 없습니다

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

은이어야한다.

또한 C의 매개 변수가 값에 의해 전달되므로 , function 등의 변경 내용은 separate_test() 안에 표시되지 않습니다. 당신은 너무 좋아, 대신 포인터 포인터를 통과 할 수 있습니다 :

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()가된다 :

나 strlen 무엇
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); 
} 
+0

@BLUEPIXY 하! 좋은 캐치. 나는 내 대답을 편집했다. –

0

seperate_testfunction 매개 변수가 임시 변수이기 때문입니다. 따라서 포인터가 가리키는 임의의 주소를 취합니다. 변수를 호출하기 전에 NULL로 초기화되기 때문입니다. Si 함수를 호출하거나 함수 매개 변수를 반환하기 전에 function = malloc(sizeof(function))을 작성하는 것이 좋습니다.

1

function의 값과 기타 변수는 separate_test입니다. 그러나 값으로 전달되기 때문에 호출 함수에서 해당 변수의 값이 변경됩니다.

관련 문제