behavior of char* in a hash

D

digz

This is a simple case of a problem I am having with using char* in a
hash_map,
The code prints the size correctly( 3) and it should print three lines
of output but is printing only two. I do not want to reserve space for
the char* using new etc..( it will never will be deepcopied using
strcpy etc..) ..it always holds an address.

I know I am making some very fundamental mistake what am I missing
here ?

#include<ext/hash_map>
#include<boost/functional/hash.hpp>
#include<iostream>
using namespace std;
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};

struct cacheType {
int i;
double d;
char* c;
cacheType():i(0),d(0.0),c(0){}
};

int main()
{
typedef boost::hash<const char*> strHashFn;
typedef __gnu_cxx::hash_map<const char*, cacheType, strHashFn,
eqstr> cacheMap;
typedef cacheMap::const_iterator It;
cacheMap cMap_;
cacheType ci_, cd_, cc_;
ci_.i = 1;
cd_.d = 2.0;
const char* c = "2007-09-11 18:00:00";
cc_.c = const_cast<char*>(c);

cMap_.insert(make_pair("A2", cc_));
cMap_.insert(make_pair("A1", cd_));
cMap_.insert(make_pair("A0", ci_));

cout << cMap_.size() << std::endl;

for( It i=cMap_.begin(); i != cMap_.end(); i++)
cout << i->first << " " << (i->second).i << " " << (i->second).d
<< " "<
< (i->second).c << std::endl;

}

Thanks
Digz
 
D

digz

This is a simple case of a problem I am having with using char* in a
hash_map,
The code prints the size correctly( 3) and it should print three lines
of output but is printing only two. I do not want to reserve space for
the char* using new etc..( it will never will be deepcopied using
strcpy etc..) ..it always holds an address.

I know I am making some very fundamental mistake what am I missing
here ?

#include<ext/hash_map>
#include<boost/functional/hash.hpp>
#include<iostream>
using namespace std;
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}

};

struct cacheType {
int i;
double d;
char* c;
cacheType():i(0),d(0.0),c(0){}

};

int main()
{
typedef boost::hash<const char*> strHashFn;
typedef __gnu_cxx::hash_map<const char*, cacheType, strHashFn,
eqstr> cacheMap;
typedef cacheMap::const_iterator It;
cacheMap cMap_;
cacheType ci_, cd_, cc_;
ci_.i = 1;
cd_.d = 2.0;
const char* c = "2007-09-11 18:00:00";
cc_.c = const_cast<char*>(c);

cMap_.insert(make_pair("A2", cc_));
cMap_.insert(make_pair("A1", cd_));
cMap_.insert(make_pair("A0", ci_));

cout << cMap_.size() << std::endl;

for( It i=cMap_.begin(); i != cMap_.end(); i++)
cout << i->first << " " << (i->second).i << " " << (i->second).d
<< " "<
< (i->second).c << std::endl;

}

Thanks
Digz

did I break some list etiquette ? is that why my question is not being
answered

Thx
Digz
 
J

Jim Langston

digz said:
did I break some list etiquette ? is that why my question is not being
answered

Most people, well me anyway, don't deal with hash maps. I know the theory
but have never found a real use for them, so never used them. To answer
your question I would have to compile your code, find out where the problem
is, then maybe have to research hash maps to be able to answer. And since
you're already using hash maps I fgiure you can do the research :D

This code does not seem to be the minimal code to reproduce the bug for a
quick analize, and if it is, then the subject is complicated which would
require even more time to research it and find the problem. If I had any
interest in ever using hash maps I'd probably take the time to figure out
what the problem is.

As it is, you did not break any list etiquette with this question. Some of
us just don't use boost and such. I don't even have boost installed to be
able to compile the code.
 
T

tony_in_da_uk

I know I am making some very fundamental mistake what am I missing
here ?
....
struct cacheType {
int i;
double d;
char* c;
cacheType():i(0),d(0.0),c(0){}
}; ....
for( It i=cMap_.begin(); i != cMap_.end(); i++)
cout << i->first << " " << (i->second).i << " " << (i->second).d
<< " "<
< (i->second).c << std::endl;

Might want to try initialising c to "" rather than printing
(char*)0...? More generally, why mix this crappy variant concept with
your hash experimentation? When you're having trouble, it's best to
do some simple usage tests with each independently, to see which half
is broken.

Cheers,

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top