동적 할당된 개체를 퀵 소트하려는데 안 되네요.
글쓴이: topeng / 작성시간: 토, 2005/06/25 - 12:45오후
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NEIGHBOR 100
typedef struct _neighbor_list {
int distance;
char* hostport;
} NeighborList;
NeighborList *neighbor_list[MAX_NEIGHBOR];
int PrintList(int n);
int NeighborCompare(const void* n1, const void* n2);
int InsertNeighbor(NeighborList element);
int PrintList(int n);
int NeighborCompare(const void* n1, const void* n2)
{
NeighborList* _n1 = (NeighborList*)n1;
NeighborList* _n2 = (NeighborList*)n2;
return (_n1->distance - _n2->distance);
}
int InsertNeighbor(NeighborList element)
{
static int count = 0;
count++;
if ( count > MAX_NEIGHBOR )
return -1;
neighbor_list[count-1] = (NeighborList*) malloc( sizeof(NeighborList) );
neighbor_list[count-1]->hostport = (char*) malloc( strlen(element.hostport)+1 );
neighbor_list[count-1]->distance = element.distance;
strcpy(neighbor_list[count-1]->hostport, element.hostport);
qsort(*neighbor_list, count-1, sizeof (NeighborList), NeighborCompare);
return count;
}
int main(int argc, char** argv)
{
int n = 0;
NeighborList *element;
element = (NeighborList*) malloc (sizeof(NeighborList));
element->hostport = (char*) malloc (100);
element->distance = 10;
strcpy(element->hostport, "abcdefg");
n = InsertNeighbor(*element);
element->distance = 5;
strcpy(element->hostport, "asdf");
n = InsertNeighbor(*element);
PrintList(n);
element->distance = 6;
strcpy(element->hostport, "oooo");
n = InsertNeighbor(*element);
PrintList(n);
element->distance = 1;
strcpy(element->hostport, "klj");
n = InsertNeighbor(*element);
PrintList(n);
return 1;
}
InsertNeighbor 함수를 이용해서 새로운 데이터를 추가함과 동시에 정렬을 합니다. 그런데, NeighborList 구조체를 정적으로 선언해서 실행하면 잘 되는데, 동적으로 메모리 할당을 하니 비교 함수(NeighborCompare)에 이상한 값이 들어오네요. 결국 소트가 제대로 안 됩니다.
Forums:


구조체를 정적으로 했을 때는 NeighborList neighbor_
구조체를 정적으로 했을 때는
NeighborList neighbor_list[MAX_NEIGHBOR];
로 선언하셨겠죠. 그런데
NeighborList *neighbor_list[MAX_NEIGHBOR];
로 선언하면 neighbor_list의 형이 달라졌으므로 함수 내용도 고쳐야 합니다.
int NeighborCompare(const void* n1, const void* n2) { const NeighborList* const* _n1 = n1; const NeighborList* const* _n2 = n2; return ((*_n1)->distance - (*_n2)->distance); } qsort(neighbor_list, count, sizeof (*neighbor_list), NeighborCompare);더불어 int InsertNeighbor(NeighborList element) 는 구조체 인자를 복사하여
넘기므로 비효율적입니다. int InsertNeighbor(const NeighborList* element) 로
하는 것이 좋습니다.
그런데 프로그램 구조를 보니 neighbor_list가 포인터의 배열일 필요가 없어 보입니다.
저라면 이렇게 할 듯.
NeighborList *neighbor_list; int NeighborCompare(const void* n1, const void* n2) { const NeighborList* _n1 = n1; const NeighborList* _n2 = n2; return (_n1->distance - _n2->distance); } int InsertNeighbor(const NeighborList* element) { static int count = 0; count++; if ( count > MAX_NEIGHBOR ) return -1; neighbor_list[count-1].hostport = malloc( strlen(element->hostport)+1 ); neighbor_list[count-1].distance = element->distance; strcpy(neighbor_list[count-1].hostport, element->hostport); qsort(neighbor_list, count, sizeof (*neighbor_list), NeighborCompare); return count; } int main(int argc, char** argv) { int n = 0; NeighborList element; // allocate memory for MAX_NEIGHBOR elements neighbor_list = malloc(sizeof(*neighbor_list) * MAX_NEIGHBOR); element.hostport = malloc (100); element.distance = 10; strcpy(element.hostport, "abcdefg"); n = InsertNeighbor(&element); /* ... */ }댓글 달기