Solved

using hash_map and string's c_str. i can't use string literals to insert ... please help!!!

Posted on 2004-04-13
6
1,008 Views
Last Modified: 2008-02-01
Hi,

The basic problem I'm having is that I need to store hashes as
hashtable[someitem->toString().c_str()] = value;

where the result of .c_str() is lets say, "mykey"
but i need to access them elsewhere using something like

hashtable.find("mykey");

Right now, it doesn't seem to work, because my hash table (i think) stores the address of c_str() instead of a literal (which I want). Anyway to convert string to a string literal :P?

Another example of a problem is like if I have
string s("number"+var);
hashtable[s] = value;

where var = "123"

later i have to get that value out of the hashtable as
hashtable.find("number123") or
string y("number"+var);

hashtable.find(y.c_str());

It ... doesn't .. work :(!!!!


Thanks.

-Edward
0
Comment
Question by:edwardt
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 10819421
Your problem might be due to the use of C_str()

ref : from
http://www.bcbdev.com/articles/suggest.htm#cstr

Don't store the result of AnsiString::c_str()

Examine the following code segment.

    AnsiString strText = "Howdy Mr. Ditka.";
    char *ptr = strText.c_str();
    strText = "Goodbye Mr. Ditka";
    Label1->Caption = ptr;

This code contains a serious defect. If you execute this code, you will see that the label displays the first string that was assigned to strText. This may surprise you. Why doesn't the label contain the string that says "Goodbye Mr. Ditka"? After all, doesn't ptr point to the string that is contained in the strText variable?

Whenever you assign a new string to an AnsiString variable, the AnsiString deallocates whatever memory it previously owned and allocates new memory for the new string. When the new memory is allocated, it is unlikely to be the same memory that was originally returned by the c_str function.
.........



altrernatively paste your code here ( if it isn't too long , and we can have a look )


/abhijit/
0
 
LVL 4

Expert Comment

by:greyfairer
ID: 10822599
Hi there,

Are you really using a hashmap/hashtable?  Then it should only accept int as argument, not char*.  OK, you can cast char* to int, but that's pointer arithmetic, not what you want, since everytime you do this, you get a different pointer

you should have a function that computes a hash code from your string:

int hash(std::string s)
{
  ...
}

Then you can use hashtable[hash(s)] and hashtable.find(hash(s)).

I found a hash function in some manual defined as follows:

static const std::collate<char>& collate_ = std::use_facet<std::collate<char> >(std::locale::classic());
return collate_.hash(s.c_str(), s.c_str()+s.size());

don't know what it does, but it works :-)


0
 
LVL 4

Expert Comment

by:greyfairer
ID: 10822618
By the way, to do string concatenation, you probably need
std::string("number")+value;
otherwise it shouldn't even compile.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:edwardt
ID: 10826248
avizit - that was a good article. any help in terms of how I can convert a string into a string literal, so that I won't have the temporaryvariable problem?

My hash is declared as

hash_map<const char*, int, hash<const char*>, eqstr > test;
       

so there's a function that works like...

foo::update(const char* str, int i){
test[str] = i;
}

and "was" called using

foo::anotherfunc(){
update(someobj1->toString().c_str(), 3);
update(someobj1->toString().c_str(), 6);

So basically, I have objects which can be represented as strings that i need to pass in. I'd prefer to convert strings into string literals if possible ;P

}

Thanks.

-Edward



0
 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
ID: 10829268
try strdup()

strdup()

duplicate string
      
Function
SYNOPSIS

#include <string.h>

char *strdup(const char *s1);
DESCRIPTION

The strdup() function allocates memory and copies into it the string addressed by s1, including the terminating null character. It is the user's responsibility to free the allocated storage by calling free().


=======

basically you can duplicate the string returned by C_str() , strdup will duplicate the string and give you an copy of the string which is NOt temporary.


/abhijit/
0
 

Author Comment

by:edwardt
ID: 10835581
Hi,

This looks good. I basically (yesterady) just made char * variables in my function and returned them to store chars. But in this case, I can probalby use this for future use.

Thanks!

-Edward
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now