Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > hash table problems being resolved

Reply
Thread Tools

hash table problems being resolved

 
 
Pieter Claassen
Guest
Posts: n/a
 
      08-16-2004
But, last few questions if I may:

Here is a test program that compiles and works


#include <search.h>
#include <stdio.h>
#include <string.h>

typedef struct {
char * name;
char * data;
} pkt;
void get_state(const pkt *a, pkt *b);

int main(){
pkt d,r;


hcreate(100);
d.name="A";
d.data="B";

get_state(&d,&r);

printf("data val is %s\n",r.data);

d.name="A";
d.data="b";

get_state(&d,&r);

printf("data val is %s\n",r.data);

hdestroy();
return(0);

}


void get_state(const pkt *a, pkt *b){
ENTRY e,*t;

e.key=a->name;
e.data=(void*)a;
t=hsearch(e,ENTER);
memcpy(b,t->data,sizeof(*b));

}


Questions:
1. This only works if I memcpy the data to a statically(?) allocated
struct (r). Why can I not just do the following in get_state:

t=hsearch(e,ENTER);
b=(pkt*)t->data;

When I do this, it is as if the data that t is pointing to is cleaned up
and b then points to garbage.

Thanks for the comments so far.

Pieter
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      08-16-2004

On Mon, 16 Aug 2004, Pieter Claassen wrote:
>
> Here is a test program that compiles and works


No, it doesn't. First, there's the non-standard header file
<search.h>, and then there are calls to undeclared functions such
as 'hcreate' and 'hsearch', and undefined types such as 'ENTRY'.
No wonder it has problems! Try posting a complete, compilable
program, or if this <search.h> is some platform-specific header,
try asking a newsgroup dedicated to your platform.

When you post code to Usenet, do not use hard tabs (ASCII 0x09).
They ruin the indentation and make it hard to read or quote properly.
Run the program through a detabber such as <blatant plug>
http://www.contrib.andrew.cmu.edu/~a...e/usenetify2.c
before posting.

Code reflowed for readability below...

> typedef struct {
> char *name;
> char *data;
> } pkt;
>
> void get_state(const pkt *a, pkt *b);
>
> int main()
> {
> pkt d, r;
> hcreate(100);
>
> d.name = "A";
> d.data = "B";
> get_state(&d, &r);


Here you are entering ("A", ("A","B")) into the hash.

> printf("data val is %s\n", r.data);


This prints "data val is B".

> d.name = "A";
> d.data = "b";


Here you are overwriting the "B" with a "b": now ("A", ("A","b"))
is in the hash, and ("A", ("A","B")) is not. If you don't understand
this, I recommend you start with standard C, and study pointers.
Work your way up to complicated things like hashtables. Even if you
already know one language, it can be tricky to apply that knowledge
to another language as dissimilar as C.

> get_state(&d, &r);


Here you are trying to insert ("A", ("A", "b")) into the table.
I can't tell, from the meager documentation I found via Google, what
this will do.

> printf("data val is %s\n", r.data);


This will either print "data val is b", or it will print nothing,
the program having already crashed (or worse) upon trying to dereference
the null pointer returned by 'hsearch'.

> hdestroy();
> return 0;
> }
>
>
> void get_state(const pkt *a, pkt *b)
> {
> ENTRY e, *t;
>
> e.key = a->name;
> e.data = (void*)a;
> t = hsearch(e, ENTER);
> memcpy(b, t->data, sizeof *b);
> }


> Questions:
> 1. This only works if I memcpy the data to a statically(?) allocated
> struct (r). Why can I not just do the following in get_state:
>
> t = hsearch(e,ENTER);
> b = (pkt*)t->data;
>
> When I do this, it is as if the data that t is pointing to is cleaned
> up and b then points to garbage.


As I said, study pointers for a while. Changing the value of the
local variable 'b' inside 'get_state' doesn't do anything to 'r' back
in 'main'. So indeed 'r' continues to hold (not "point to") garbage.

I recommend K&R2, "The C Programming Language." If you're doing
hash-tables already, you probably have enough experience to understand
their style of exposition.

HTH,
-Arthur

 
Reply With Quote
 
 
 
 
Pieter Claassen
Guest
Posts: n/a
 
      08-16-2004
On Mon, 16 Aug 2004 12:06:04 -0400, Arthur J. O'Dwyer wrote:

>
> On Mon, 16 Aug 2004, Pieter Claassen wrote:
>>
>> Here is a test program that compiles and works

>
> No, it doesn't. First, there's the non-standard header file
> <search.h>, and then there are calls to undeclared functions such


Points taken.

My pointer logic resulted in a terribly non-functional demonstration.

Is there a way to "dump" the stack and heap memory of a process,
specifically to see what the members of structs are?

Thanks,
Pieter


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Re: Resolved: Re: An assignment isn't being executed. Fred C Programming 5 10-06-2011 12:37 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Problems with external USB drive - resolved! Rustiferion Computer Support 1 09-03-2005 08:27 AM
standard library for hash table storage and hash algorithm Pieter Claassen C Programming 1 08-04-2004 03:11 AM



Advertisments