2013-11-04 2 views
0

최근에 저는 서로 다른 데이터 구조를 코딩하여 프로그래밍 기술을 향상시켜 왔습니다.연결된 목록 작업 (코어 덤프)

이제 연결 목록을 작성하고 있지만이 오류에 대해 잘 모르겠으므로 문제가 발생하여 오랜 시간 짜증이났다. 분할 오류 (코어 덤프)는 알았지 만 나는 기억의 작동에있어 잘못된 것을 만들었다.

link_list.h :

struct LINK_LIST { 
    char *string; 
    struct LINK_LIST *next; 
}link_list; 

==============================

link_list 이 .c :

int insert(struct LINK_LIST *link, int pos, char *in) { 
    int i; 
    if (get_length(link)>=STRING_SIZE) { 
    fprintf(stderr, "Link list is full!!!"); 
    return ERROR; 
    } 
    else { 
    if (pos < 0 || pos-1 > get_length(link)) { 
     fprintf(stderr, "Invalid position"); 
     return ERROR; 
    } 
    else { 
     i = 0; 
     do { 
      struct LINK_LIST *new_node; 
      init_link_list(new_node); 
      new_node->next = link->next; 
      link->next = new_node; 
      new_node->string = in; 
      i += 1; 
     } while(i<pos-1); 
    } 
} 
return OK; 
} 
+0

첫 번째 작업은'='의 오른쪽에있는 (struct LINK_LIST *) 캐스트를 제거하는 것입니다. – Bathsheba

+0

다음 지침을 따라야합니다. http://sscce.org/ 가장 작은 실패 사례를 게시하십시오. –

+0

'init_link_list (new_node);'이후,'new_node' 포인터는 아직 초기화되지 않습니다. – aschepler

답변

2

당신에게 :

여기
#include<stdio.h> 
#include<stdlib.h> 

int init_link_list(struct LINK_LIST *new_link) { 
    //char *new_string; 
    int i; 
    //new_string = (char *)malloc(sizeof(char) * STRING_SIZE); 
    new_link = (struct LINK_LIST *)malloc(sizeof(struct LINK_LIST)); 
    if (new_link==NULL) { 
fprintf(stderr, "Insufficient memory!!!"); 
return ERROR; 
    } 
    //new_link->string = new_string; 
    new_link->string = NULL; 
    //new_link->next = NULL; 

    return OK; 
} 

나는 다음 삽입 작업, 초기화 작업을 정의 버그가 가지고

new_link = (struct LINK_LIST *)malloc(sizeof(struct LINK_LIST)); 

하지만 수정 함수로 로컬이다 : init_link_list에서

struct LINK_LIST *new_node; 
init_link_list(new_node); 

를 인수의 값을 수정 당신이 당신의 전화 기능에 다시 도착하면, 그 변화를 분실 :

struct LINK_LIST *new_node; 
init_link_list(new_node); 
// Oops ! new_node's new value is lost ! 

당신은 메모리 누수가합니다 (malloc의 결과는 손실) 및 new_node가 초기화되지 않았습니다. *new_node에 액세스하려고하면 메모리의 임의 위치에 액세스하므로 코어 덤프가 발생합니다. 실패한 경우

몇 가지 가능한 수정이 있습니다, 가장 쉬운 방법은 OK/ERROR 반환 값을 삭제하고 malloc에 ​​성공하면 널이 아닌 포인터 중 하나를 반환, 또는 NULL입니다 : 다음

struct LINK_LIST *init_link_list(void) { 
    struct LINK_LIST *new_link = malloc(sizeof(struct LINK_LIST)); 

    if (new_link==NULL) { 
    fprintf(stderr, "Insufficient memory!!!"); 
    return NULL; 
    } 

    new_link->next = NULL; 
    return new_link; 
} 

코드

... 
else { 
    i = 0; 
    do { 
     struct LINK_LIST *new_node = init_link_list(); 
     // Note : here, should check whether new_node is NULL and deal with the situation 
     new_node->next = link->next; 
     link->next = new_node; 
... 
+0

공유 할 수있는 좋은 코드를 제공해주십시오. – jacouh

+0

하지만'new_link'가 다시 선언되었습니다! – hyaocuk

+0

@hyaocuk, 아니요, 'new_link'는 다시 선언되지 않습니다; 'insert'의 값은'init_link_list'의 값과 같지 않습니다. 그들은 같은 이름을 가지지 만 우연의 일치입니다 (두 가지 기능 중 하나에서 이름을 바꿀 수 있습니다. 내 대답에'init_link_list'의 새 버전을 호출하는 코드를 추가했습니다. 요점을 이해하는 데 도움이되기를 바랍니다. – Fabien