473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

behavior of char* in a hash

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,
eqstrcacheMap;
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

Sep 13 '07 #1
3 2618
On Sep 12, 8:37 pm, digz <Digvijo...@gmail.comwrote:
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,
eqstrcacheMap;
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

Sep 13 '07 #2
"digz" <Di********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On Sep 12, 8:37 pm, digz <Digvijo...@gmail.comwrote:
>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,
eqstrcacheMap;
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;

}

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.
Sep 13 '07 #3
On Sep 13, 9:37 am, digz <Digvijo...@gmail.comwrote:
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

Sep 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
3
by: railrulez | last post by:
Hi, Attached is a program which uses a hash_set, but I cant seem to get find() or iterators working on it. I'm not sure whether hash_set is std C++, but I dont know where else to ask. ...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
2
by: Henrik Goldman | last post by:
Hi, I'd like to use std::set to get a uniqueness between a set of (unsigned char *) with fixed known lengths. So I thought I would write my own class which holds this: class CUnique {
44
by: gokkog | last post by:
Hi there, There's a classic hash function to hash strings, where MULT is defined as "31": //from programming pearls unsigned int hash(char *ptr) { unsigned int h = 0; unsigned char *p =...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
11
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
11
by: merrittr | last post by:
Hi in the code below in the main the hash table from K&R example replaces a node when a collision occurs What I am trying to do is set it up to "chain" another structure beside the node that it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.