2014-09-23 2 views
2

C에서 간단한 단일 링크드 목록을 만들고 내 프로그램을 실행하는 동안 무한 "Singal 11 being dropped"루프가 발생했습니다 Valgrind에서.링크 된 목록에 추가하는 동안 Valgrind와 함께 무한 "신호 11 떨어 뜨림"루프가 발생했습니다.

내 .H 파일 :

#ifndef TEST_H 
#define TEST_H 

struct fruit { 
    char name[20]; 
}; 

struct node { 
    struct fruit * data; 
    struct node * next; 
}; 

struct list { 
    struct node * header; 
    unsigned count; 
}; 

#endif 

내 .c 파일 :

#include "test.h" 
#include <stdio.h> 
#include <string.h> 

void init_list(struct list my_list) 
{ 
    my_list.header = NULL; 
    my_list.count = 0; 
} 

void add_to_list(struct list my_list, struct fruit my_fruit) 
{ 
    struct node my_node; 
    struct node nav_node; 

    my_node.data = &my_fruit; 
    my_node.next = NULL; 

    if(my_list.count == 0) { /* set head node if list is empty */ 
     my_list.header = &my_node; 
     my_list.count++; 
    } else { 
     nav_node = *my_list.header; 

     while (nav_node.next != NULL) { /* traverse list until end */ 
      nav_node = *nav_node.next; 
     } 

     nav_node.next = &my_node; 

     my_list.count++; 
    } 

} 

int main() 
{ 
    struct fruit fruit_array[5]; 
    struct list fruit_list; 
    int i; 

    strcpy(fruit_array[0].name, "Apple"); 
    strcpy(fruit_array[1].name, "Mango"); 
    strcpy(fruit_array[2].name, "Banana"); 
    strcpy(fruit_array[3].name, "Pear"); 
    strcpy(fruit_array[4].name, "Orange"); 

    init_list(fruit_list); 

    for(i=0; i < 5; i++) { 
     add_to_list(fruit_list, fruit_array[i]); 
    } 

    return 0; 
} 

나는 문제가 add_to_list 내 목록 탐색에서 유래 있으리라 믿고있어,하지만 난 것에 대해 확실 해요 I 잘못하고있어.

감사합니다.

답변

1

값으로 구조체를 함수에 전달하고 있습니다. 그러면 함수에 구조체의 복사본이 만들어지고 호출에 대한 변경 내용은 구조체에서 발생하지 않습니다.

좋아하는 C 언어 책에서 포인터에 관해 읽어야합니다.