RE: GHashTable insertion problems (string literals, c_str(), etc.)



Hi,

 

I’ve narrowed down the issue a bit more.

 

The problem is when I have

 

 

Foo::update(const char * str, MyObj *obj){

g_hash_table_insert(table, (char*)str, obj);

 

}

 

and I’m calling it with

 

update(someobj->toString().c_str(), obj);

update(someobj2->toString().c_str(), obj2);

 

When I try to insert multiple objects of c_str(), then

 

Is there anyway I can store in / GHashTable takes in strings as *string literals* ? The issue I come across when after I insert two of these types of objects, and I run g_hash_table_foreach(), then the loop keeps on repeating itself infinitely,  I believe because of something due to using c_str() as the key.

 

Thanks.

 

-Edward

 

 

-----Original Message-----
From: gtk-list-admin gnome org [mailto:gtk-list-admin gnome org] On Behalf Of edwardt
Sent:
Tuesday, April 13, 2004 1:20 PM
To: gtk-list gnome org
Subject: GHashTable insertion problems

 

Hi,


I have problems in doing insertions into my GHashTable

 

Lets say I have my table

 

Class Foo{

GHashTable *table;

}

 

foo::foo(){

table = g_hash_table_new(g_str_hash, g_str_equal);

}

 

 

I have functions like

 

Foo::update(Address *addr, MyObj *obj){

g_hash_table_insert(table, (char*)addr->toString().c_str(), obj);

 

}

 

and in another function

 

Foo::dostuff(){

 

update(addr1, obj1);

update(addr2, obj2);

 

}

 

Each time I call update, it changes the hash table so that lets say addr1->toString().c_str() gave “helloaddress” first, the second time I call it, where it becomes “byebyeaddress” (addr2….), the items in the hash table change.

 

It seems like the hash table just copies pointers – is that true? If so, how else can I modify this so I can store the correct key and values, without concern that the keys and values will change outside of modifying the hash table?

 

Thanks.

 

-Edward



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]