[질문] hcreate_r 써보신분?... 에러좀 고쳐주세요..
#define _GNU_SOURCE
#include
#include
#include
/*
typedef struct entry
{
char *key;
void *data;
}
ENTRY;
*/
int main(int argc,char *argv[])
{
char a[256];
int read_num;
char *item;
char *insert_item;
int item_size;
int result;
ENTRY entry;
struct hsearch_data root; // hash root
result = hcreate_r(100,&root);
if ( result == 0 ) {
perror("hash create error");
exit(1);
}
while ( 1 ) {
printf(" input ...");
fflush(stdout);
read_num = read(0,a,256);
if ( read_num == 0 ) break;
if ( read_num == 1 && a[0] == '\n' ) {
break;
}
if ( read_num < 0 ) {
perror("read error");
exit(1);
}
a[read_num-1] = '\0';
item_size = strlen(a);
printf("size = %d\n",item_size);
item = (char *)malloc(item_size+1);
if ( item == NULL ) {
perror("memory error");
exit(1);
}
strncpy(item,a,item_size+1);
printf("before inserting item is %s(%p)\n",item,item);
entry.key = item;
entry.data = item;
result = hsearch_r(entry,ENTER,NULL,&root);
if ( result == 0 ) {
perror("insert failure");
}
printf("after inserting item is %s(%p)\n",*(char**)
insert_item,insert_item);
printf("root pointer is %p \n",&root);
}
printf("result...\n");
hdestroy_r(&root);
}
해쉬 함수를 C 에서도 쓸수가 있더군요.
근데 제가 작성한 테스트 함수에서는요..
hcreate_r 부분에서 계속 return 값이 0 이 나오고 hash creation error
가 나오네요.
실제로 exit(1) 부분을 빼고 하면 core dump 가 생기고요..
물론... root 를 포인터 형으로 선언하고 해봤는데..
똑같은 현상이 생기네요..
어떻게 해야 될까요?.
아님.. 다른 test 소스를 보여주셨음 감사하겠습니다...
즐거운 하루.~~~
Re: [질문] hcreate_r 써보신분?... 에러좀 고쳐주세요..
자답이네요..
struct hsearch_data root;
root.table = NULL;
이렇게 초기화 해 줘야 하는군요..
그리고
result = hsearch_r(entry,ENTER,NULL,&root);
여기서 NULL 을 넣으면 안되고..
ENTRY ** 형을 직접 넣어줘야 하네요...
잘 해결 됐네요...
임동현 wrote..
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <search.h>
/*
typedef struct entry
{
char *key;
void *data;
}
ENTRY;
*/
int main(int argc,char *argv[])
{
char a[256];
int read_num;
char *item;
char *insert_item;
int item_size;
int result;
ENTRY entry;
struct hsearch_data root; // hash root
result = hcreate_r(100,&root);
if ( result == 0 ) {
perror("hash create error");
exit(1);
}
while ( 1 ) {
printf(" input ...");
fflush(stdout);
read_num = read(0,a,256);
if ( read_num == 0 ) break;
if ( read_num == 1 && a[0] == '\n' ) {
break;
}
if ( read_num < 0 ) {
perror("read error");
exit(1);
}
a[read_num-1] = '\0';
item_size = strlen(a);
printf("size = %d\n",item_size);
item = (char *)malloc(item_size+1);
if ( item == NULL ) {
perror("memory error");
exit(1);
}
strncpy(item,a,item_size+1);
printf("before inserting item is %s(%p)
\n",item,item);
entry.key = item;
entry.data = item;
result = hsearch_r(entry,ENTER,NULL,&root);
if ( result == 0 ) {
perror("insert failure");
}
printf("after inserting item is %s(%p)\n",*
(char**)
insert_item,insert_item);
printf("root pointer is %p \n",&root);
}
printf("result...\n");
hdestroy_r(&root);
}
해쉬 함수를 C 에서도 쓸수가 있더군요.
근데 제가 작성한 테스트 함수에서는요..
hcreate_r 부분에서 계속 return 값이 0 이 나오고 hash creation error
가 나오네요.
실제로 exit(1) 부분을 빼고 하면 core dump 가 생기고요..
물론... root 를 포인터 형으로 선언하고 해봤는데..
똑같은 현상이 생기네요..
어떻게 해야 될까요?.
아님.. 다른 test 소스를 보여주셨음 감사하겠습니다...
즐거운 하루.~~~
댓글 달기