P
Pieter Claassen
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
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