[질문] 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 소스를 보여주셨음 감사하겠습니다...

즐거운 하루.~~~

익명 사용자의 이미지

자답이네요..

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 소스를 보여주셨음 감사하겠습니다...

즐거운 하루.~~~

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.