Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > hashtable...squid

Reply
Thread Tools

hashtable...squid

 
 
s88
Guest
Posts: n/a
 
      05-31-2005
I work with the bit-vector(http://search.cpan.org/dist/Bit-Vector/)
and squid lib as well. But I meet a weird situation now...Let's see my
code.
I wanna creat a hashtable in a function, and then return the address of
the hashtable.
But my hashtable looks empty outside the fake_table().
The hashtable just manage the pointer I insert. Don't really "copy"
anything into the table.


hashtable *fake_table(){

BitVector_Boot();
N_char x1[] = "101000";
N_char x2[] = "101010";

wordptr X1 = BitVector_Create(6, 1);
wordptr X2 = BitVector_Create(6, 1);

BitVector_from_Bin(X1, x1);
BitVector_from_Bin(X2, x2);

hash_table *inst_table =
hash_create(inst_tb_compare,7951,hash_string);
hash_table *ht2 =
hash_create(inst_tb_compare,7951,hash_string);

inst_tb_atom *_atom1 = (inst_tb_atom
*)malloc(sizeof(inst_tb_atom));
inst_tb_atom *_atom2 = (inst_tb_atom
*)malloc(sizeof(inst_tb_atom));

_atom1->_type = 1;
_atom1->point_to.ht = ht2;
_atom1->X = X1;

_atom2->_type = 1;
_atom2->point_to.ht = ht3;
_atom2->X = X2;

hash_insert(ht2,x2,_atom2);
hash_insert(inst_table,x1,_atom1);

return inst_table;
}

Thanx!!!

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      05-31-2005
On 30 May 2005 17:45:29 -0700, "s88" <(E-Mail Removed)> wrote in
comp.lang.c:

> I work with the bit-vector(http://search.cpan.org/dist/Bit-Vector/)
> and squid lib as well. But I meet a weird situation now...Let's see my
> code.
> I wanna creat a hashtable in a function, and then return the address of
> the hashtable.
> But my hashtable looks empty outside the fake_table().
> The hashtable just manage the pointer I insert. Don't really "copy"
> anything into the table.


The problem here is that your code is littered with non-standard types
that are apparently defined (with typedef or macros) in the header
files for the third party libraries that you are trying to use.

> hashtable *fake_table(){
>
> BitVector_Boot();


The C language does not define what this function call does, so there
is no reason at all to assume that anyone here knows either.

> N_char x1[] = "101000";
> N_char x2[] = "101010";


Here you appear to be defining two arrays of type 'N_char', which must
be an alias for one of the character types, after calling a function.
Does your compiler conform to the 1999 or later version of the C
standard? This is illegal code in earlier versions of C.


> wordptr X1 = BitVector_Create(6, 1);
> wordptr X2 = BitVector_Create(6, 1);
>
> BitVector_from_Bin(X1, x1);
> BitVector_from_Bin(X2, x2);
>
> hash_table *inst_table =
> hash_create(inst_tb_compare,7951,hash_string);
> hash_table *ht2 =
> hash_create(inst_tb_compare,7951,hash_string);
>
> inst_tb_atom *_atom1 = (inst_tb_atom
> *)malloc(sizeof(inst_tb_atom));


Casting the pointer returned by malloc() in C is unnecessary and can
hide a serious error causing undefined behavior if <stdlib.h> is not
included or there is otherwise not a proper prototype for malloc() in
scope.

Also you are committing the serious error of not checking the return
value of malloc() for NULL, which it will return if the allocation
fails.


> inst_tb_atom *_atom2 = (inst_tb_atom
> *)malloc(sizeof(inst_tb_atom));
>
> _atom1->_type = 1;
> _atom1->point_to.ht = ht2;
> _atom1->X = X1;
>
> _atom2->_type = 1;
> _atom2->point_to.ht = ht3;
> _atom2->X = X2;
>
> hash_insert(ht2,x2,_atom2);
> hash_insert(inst_table,x1,_atom1);
>
> return inst_table;
> }
>
> Thanx!!!


We haven't got the faintest idea of how these functions get the memory
they use. You'll have to talk to whoever made the library.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
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




Advertisments