2016-12-02 1 views
-1

우선 서식 및 어려운 코드에 대해 사과 드리겠습니다. 나는 C와 스택에 익숙하지 않다. 지저분한 코드의 대부분은 문제와 관련이 없지만 컨텍스트에 포함해야합니다.realloc 중 세그먼트 오류

realloc에 ​​대한 첫 번째 호출 (주석에 명시된) 이후에 세그먼테이션 결함이 발생합니다. return_file->target_line은 단순히 3D 배열이며 i은 3D 배열의 첫 번째 차원의 요소 개수입니다. 그래서 추가의 2D 배열 (타입 char **)을 저장하기 위해 realloc을 호출합니다.

NULL b/c 개발 프로토콜이 구체적으로 모든 메모리 할당이 성공할 것이라고 명시된 경우 (나는 의심 스럽습니다).

저는 자체 메모리 검사 프로그램을 사용하고 있습니다. 내가 오류 코드는 다음과 같습니다

Program received signal SIGSEGV, Segmentation fault. 
0x00007ffff7ac63fb in reallochook() from /lib64/libc.so.6 

나는 아주 긴 시간 동안 그것을보고했지만 문제가 무엇인지 찾을 수 없습니다.

Mockfile *read_mockfile(const char filename[]) { 
    Mockfile *return_file = NULL; 
    FILE *input; 

    if(filename != NULL && (input = fopen(filename, "r")) != NULL) { 
    char **split_tmp, line[MAX] = {0}; 

    return_file = malloc(sizeof(Mockfile)); 
    return_file->rule_count = 0; 

    /*read lines*/ 
    while(fgets(line, MAX, input) != NULL){ 
     if(line[0] != '#' && line[0] != '\n'){ 
     int j, i = return_file->rule_count; 
     split_tmp = split(line); 

     if(line[0] != '\t'){ 
      j = 0; 


      /*target line. Realloc every string in three steps. Segementation fault occurs after this line below.*/ 
      return_file->target_line = realloc(return_file->target_line, (i + 1) * sizeof(char **)); 

      while(split_tmp[j] != NULL){ 
      return_file->target_line[i] = realloc(return_file->target_line[i], (j + 1) * sizeof(char *)); 
      return_file->target_line[i][j] = malloc(strlen(split_tmp[j]) + 1); 
      strcpy(return_file->target_line[i][j], split_tmp[j]); 
      j++; 
      } 
      return_file->target_line[i] = realloc(return_file->target_line[i], (j + 1) * sizeof(char *)); 
      return_file->target_line[i][j] = NULL; 
     } else { 
      j = 0; 

      /*action line. Allocate every string in three steps*/ 
      return_file->action_line = realloc(return_file->action_line, (i + 1) * sizeof(char **)); 

      while(split_tmp[j] != NULL){ 
      return_file->action_line[i] = realloc(return_file->action_line[i], (j + 1) * sizeof(char *)); 
      return_file->action_line[i][j] = malloc(strlen(split_tmp[j]) + 1); 
      strcpy(return_file->action_line[i][j], split_tmp[j]); 
      j++; 
      } 
      return_file->action_line[i] = realloc(return_file->action_line[i], (j + 1) * sizeof(char *)); 
      return_file->action_line[i][j] = NULL; 

      return_file->rule_count++; 
     } 
     } 
    } 
    fclose(input); 
    } 
    return return_file; 
} 
+0

@MD XF, 그냥 중괄호가 새 라인에없는 것에 대해 이야기하고 있습니까? b/c 그건 내가 의도적으로 지키는 개인적인 취향이다. –

+0

당신에게 효과가있는 것은 무엇이든 ... 당신을 위해 일합니다. 그러나 몇 가지 중요한 개선 사항이 있습니다. 작은 차이로 큰 차이가 있습니다. –

답변

5

realloc()는 첫 번째 인자는 메모리 또는 NULL의 유효한 블록을 가리 키도록 기대하고, 그래서 malloc() 후에는 초기화해야합니다

return_file = malloc(sizeof(Mockfile)); 
return_file->rule_count = 0; 
return_file->target_line = NULL; /* Add this */ 

이 그 충돌을 해결해야한다.

foo = realloc(foo, N);realloc()NULL을 반환 할 수 있으므로 버그이므로 완료를 위해이를 처리해야합니다.

+0

이유가 무엇입니까? 다른 할당 기능이 필요합니까? 즉'malloc()'과'calloc()'? –

+1

"어느"당신이 말하는 겁니까? C 표준은 [이 페이지에 명시된대로] 동작을 정의합니다 (http://en.cppreference.com/w/c/memory/realloc). 'NULL' 리턴 값 검사는 물론 malloc()에도 필요하지만 다른 이유가 있습니다. –