Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with hash_map

Status
Not open for further replies.

smellykat

Programmer
Mar 4, 2002
33
US
Hello, I am having trouble working with hash_map. I am adding a key-value pair to the map, but when I exit the function it disappears. Here is my code:

SymTabErr SymTabMgr::enter(SymTabEntry* se)
{
SymTab *table;
SymTab st;
SymTabEntry *entry;
size_t x;

mainStack->pop(table);
x = table->count(se->name());
st = *table;
if (x == 0){ //this element doesnt exist yet
st[se->name()] = se; //add se to the hash map
mainStack->push(&st);
return SYMTAB_OK;
}
}

SymTab is a hash_map, which is a collection of SymTabEntry values. The keys are the "names" of the SymTabEntry that is being added. "st[se->name()] = se;" is the line that adding the new data. For the life of this function the data is there, but when I reenter this function the hash_map is empty!
The a pointer to the hash_map is being saved on a global stack.

Thanks for the help.
 
Now, assuming that it isn't a typo, st is a local variable that is a copy of the hash_map pointed to by table. Because it is a local variable, it is destroyed when the function exits and created new again the next time the function is called.

If you don't want the hash_map to be empty when you re-enter the function, you should add the value to table itself. You can do this:
Code:
[red](*[/red]table[red])[/red][se->name()] = se;
or if you want to keep st do this:
Code:
SymTab[red]&[/red] st = *table;
if (x == 0){ //this element doesnt exist yet
        st[se->name()] = se;    //add se to the hash map
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top