2011년7월13일_연결리스트(linked list)예제 임베디드C p.280~와 동적할당 받은 연결리스트의 해제

 

● 세 개의 구조체로 구성된 연결 리스트를 만드는 프로그램...p10-2.c

   1:  // 세 개의 구조체로 구성된 연결 리스트를 만드는 프로그램
   2:  #include <stdio.h>
   3:   
   4:  typedef struct list {
   5:      int data;
   6:      struct list *next;
   7:  } LIST;
   8:   
   9:  int main()
  10:  {
  11:      LIST a;
  12:      LIST b;
  13:      LIST c;
  14:   
  15:      a.data = 3;
  16:      b.data = 4;
  17:      c.data = 5;
  18:      
  19:      //a.next = b.next = c.next = NULL; //아래와 같이 풀어쓰라...
  20:      a.next = NULL;
  21:      b.next = NULL;
  22:      c.next = NULL;
  23:   
  24:      printf("a: %d,\tb: %d,\tc: %d\t\n", a.data, b.data, c.data);
  25:   
  26:      a.next = &b;        //a구조체next가 b구조체를 가리킴.
  27:      b.next = &c;        //b구조체next가 c구조체를 가리킴.
  28:   
  29:      printf("a: %d,\tb: %d \n", a.data, a.next->data);
  30:   
  31:      return 0;
  32:  }
  33:      


<실행결과>

image


<소스분석>

image

소스분석은 그림으로 대신하지...하하하



● 간단한 연결리스트의 작성...p10-3.c

   1:  //p0903.c: 간단한 연결리스트를 작성하는 프로그램
   2:  #include <stdio.h>
   3:  #include <stdlib.h>            //malloc()
   4:   
   5:  typedef struct node 
   6:  {
   7:      char data;
   8:      struct node *next;
   9:  } NODE;
  10:   
  11:  int main()
  12:  {
  13:      NODE *list, *temp;
  14:   
  15:      list = (NODE *)malloc(sizeof(NODE));            //동적할당 list가 가리킴.
  16:      list->data = 'a';                                //할당받은 구조체.data에 'a'대입
  17:      list->next = (NODE *)malloc(sizeof(NODE));        //동적할당 list가 가리키는 구조체의 next가 가리킴.
  18:      list->next->data = 'b';                        //그 구조체.data에 'b'대입
  19:      //동적할당 list가 가리키는 구조체.next가 가리키는 구조체의 next가 가리킴.
  20:      list->next->next = (NODE *)malloc(sizeof(NODE));    
  21:      list->next->next->data = 'c';                    //그 구조체.data에 'c'대입
  22:      list->next->next->next = NULL;                    //마지막 구조체의 next는 NULL로 초기화
  23:   
  24:      //연결리스트의 출력
  25:      temp = list;
  26:   
  27:      while(temp != NULL)        //temp가 NULL이 아닐 때 까지...
  28:      {                        
  29:          printf("%5c\n", temp->data);        //temp가 가리키는 구조체.data출력
  30:          temp = temp->next;        //temp가 가리키는 구조체의 next의 값이 대입된다.
  31:      }                            //즉, 다음 구조체로 넘어가게 된다.
  32:   
  33:      return 0;
  34:  }
  35:      
  36:   


<실행결과>

image


<소스분석>

image







● 임시변수를 이용한 연결리스트의 작성...p10-4.c

   1:  //간단한 연결리스트를 작성하는 프로그램
   2:  #include <stdio.h>
   3:  #include <stdlib.h>            //malloc()
   4:   
   5:  typedef struct node 
   6:  {
   7:      char data;
   8:      struct node *next;
   9:  } NODE;
  10:   
  11:  int main()
  12:  {
  13:      int A;
  14:      NODE *list;
  15:      NODE *temp;                                        //line #1
  16:   
  17:      printf("%08X\n", &A);
  18:      *((int *)(0xBFFFF848)) = 100;
  19:      printf("%d\n", A);
  20:      
  21:      list = (NODE *)malloc(sizeof(NODE));            //line #2
  22:      list->data = 'a';                                //line #3
  23:      temp = list;                                    //line #4
  24:      
  25:      printf("%08X heap영역 1 node\n", temp);
  26:      
  27:      temp->next = (NODE *)malloc(sizeof(NODE));        //line #5
  28:      temp = temp->next;                                //line #6
  29:      temp->data = 'b';                                //line #7
  30:      
  31:      printf("%08X heap영역 2 node\n", temp);
  32:      
  33:      temp->next = (NODE *)malloc(sizeof(NODE));        //line #8
  34:      temp = temp->next;                                //line #9
  35:      temp->data = 'c';                                //line #10
  36:      //((NODE *)(0x080497F0))->data = 'd';            //line #10-1
  37:   
  38:      printf("%08X heap영역 마지막 node\n", temp);
  39:      printf("%08X stack영역 포인터변수 temp의 주소\n", &temp);
  40:      
  41:      temp->next = NULL;                                //line #11
  42:   
  43:      //연결리스트의 출력
  44:      temp = list;
  45:   
  46:      while(temp != NULL)
  47:      {
  48:          printf("%5c\n", temp->data);
  49:          temp = temp->next;
  50:      }
  51:   
  52:      return 0;
  53:  }
  54:      
  55:   


<실행결과>

image


<소스분석> 

image  




동적할당 연결리스트의 구조체들의 자원을 운영체제에 반환하기



예제 p10-4.c 소스코드에서, 연결리스트를 해제하는 코드

   1:      //연결리스트 메모리해제, 생성 순서의 반대로 소멸시킨다.
   2:      free(list->next->next);
   3:      free(list->next);
   4:      free(list);
   5:      
   6:      return 0;

최초생성된 구조체를 가리키는 포인터변수 list를 먼저 소멸시키면,
후에 연결된 구조체를 엑세스 할 수 없게 된다. 그러니 free( )로 소멸시킬 수도 없다. 주의할 것!! (세선이 한테 배운거 ㅠㅠ)


image

그런데 제대로 소멸되었는지 어떻게 확인하지?


# man 3 free

image

free( )는 어떤 값도 반환하지 않는 void형 리턴이구나 –_—;  난감….