2012-04-08 3 views
2

일련의 프로세스 식별자와 일부 상태 정보가 포함 된 이중 연결 목록이있는 파일이 있습니다.C 프로그래밍 연결된 목록 및 제거

{ 
struct pr7_process *cur; 
    for(cur = list->head; cur != NULL; cur = cur->next) 
    { 
     if (cur->pid == pid) 
     { 
      printf("cur pid: %d\n", cur->pid); 
      cur->state = STATE_NONE; 
      if(list->head == list->tail) 
     { 
      free(cur); 
     } 
     else 
      { 
      cur->prev->next = cur->next; 
      cur->next->prev = cur->prev; 
      free(cur); 
      } 
      break; 
     } 
    } 
    } 

내 제거 기능에 어떤 문제가 :

struct pr7_process 
{ 
    pid_t pid;  /* process ID, supplied from fork() */ 
       /* if 0, this entry is currently not in use */ 
    int state;  /* process state, your own definition */ 
    int exit_status; /* supplied from wait() if process has finished */ 
    struct pr7_process *next; // a pointer to the next process 
    struct pr7_process *prev; 
}; 

/* the process list */ 

struct process_list 
{ 
    struct pr7_process *head; 
    struct pr7_process *tail; 
}; 

내 목록의 요소를 제거하는 방법이 있나요? 내 목록을 인쇄하려고 할 때 무한 루프가 발생하는 것 같습니다. 이전에 나는 그것이 무료()를 사용하는 방법이라고 생각했지만 응답에서 분명히 아니었다. :)

고마워!

+0

: 즉 같은 뭔가를 구축 감안할. –

+1

** 어떻게 ** 할당 되었습니까? –

답변

1

노드 세트 nextNULL에 추가 할 때.

그럼 모두 무료로 할 때까지 다음 == NULL까지 무료입니다.

노드를 제거 할 때. 링크 및 빈 노드를 업데이트하십시오.

또한; NULL은 무료입니다.

Valgrind는 이러한 작업을 할 때 귀중한 도구입니다.


더 많은 검사가 필요하다고 생각합니다. 즉 :

struct pr7_process { 
    int pid; 
    ... 
} const new_proc = { 
    0, 44, 0, NULL, NULL 
}; 

void del(struct process_list *list, int pid) 
{ 
    struct pr7_process *cur; 

    for (cur = list->head; cur != NULL; cur = cur->next) { 
     if (cur->pid == pid) { 

      printf("cur pid: %d\n", cur->pid); 

      if(list->head == list->tail) { 
       free(cur); 
       list->head = NULL; 
       list->tail = NULL; 
      } else if (cur == list->head) { 
       list->head = list->head->next; 
       free(cur); 
       list->head->prev = NULL; 
      } else if (cur == list->tail) { 
       list->tail = cur->prev; 
       free(cur); 
       list->tail->next = NULL; 
      } else { 
       cur->prev->next = cur->next; 
       cur->next->prev = cur->prev; 
       free(cur); 
      } 
      break; 
     } 
    } 
} 

는 일반적으로 당신은 당신이 목록에 삽입 모두를 할당하는 malloc을 사용하여 그것을 극복 목록을

int push(struct process_list *list, int pid, int state) 
{ 
    if (list->head == NULL) { /* or move this to where ever you see fit */ 
     if ((list->head = malloc(sizeof(struct pr7_process))) == NULL) 
      return -1; 
     list->tail = list->head; 
     *list->tail = new_proc; 
    } else { 
     if ((list->tail->next = malloc(sizeof(struct pr7_process))) == NULL) 
      return -1; 
     *list->tail->next = new_proc; 
     list->tail->next->prev = list->tail; 
     list->tail = list->tail->next; 
    } 
    list->tail->pid = pid; 
    list->tail->state = state; 

    return 0; 
} 

void wipe(struct process_list *list) 
{ 
    struct pr7_process *node = list->tail; 

    while (node != list->head) { 
     node = list->tail->prev; 
     free(list->tail); 
     list->tail = node; 
    } 
    free(list->head); 
    list->head = NULL; 
    list->tail = NULL; 
} 

void prnt(struct process_list list, int dir) 
{ 
    if (dir == 1) { 
     while (list.head != NULL) { 
      printf("%4d: %d\n", list.head->pid, list.head->state); 
      list.head = list.head->next; 
     } 
    } else { 
     while (list.tail != NULL) { 
      printf("%4d: %d\n", list.tail->pid, list.tail->state); 
      list.tail = list.tail->prev; 
     } 
    } 
} 

int main(void) 
{ 
    struct process_list list = {NULL, NULL}; 

    push(&list, 331, 2); /* if(push() != -1) ... */ 
    push(&list, 332, 66); 
    push(&list, 333, 47); 

    prnt(list, 1); 

    del(&list, 332); 
    prnt(list, 1); 

    wipe(&list); 
    prnt(list, 1); 

    return 0; 
} 
0

malloc에서 할당하지 않은 것을 무료로 사용할 수 없다는 것을 알고 있습니다. 어떻게 이것을 극복합니까?

무엇을 극복해야합니까? 동적으로 할당 된 항목이 있고 free()이 필요하거나 자동 저장 기간이 할당되었지만 할당하지 않았습니다. 여기에는 문제가 없습니다.

일반적으로 이와 같이 불이 켜지면 malloc 모든 것이 안정적으로 해제 될 수 있습니다. 그렇지 않으면 할당 된 방법을 모르고 정의되지 않은 동작을 실행할 수 있습니다.