473,831 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
using namespace std;
struct eqstr
{
bool operator()(cons t 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<con st 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(ma ke_pair("A2", cc_));
cMap_.insert(ma ke_pair("A1", cd_));
cMap_.insert(ma ke_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 2643
On Sep 12, 8:37 pm, digz <Digvijo...@gma il.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<iostre am>
using namespace std;
struct eqstr
{
bool operator()(cons t 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<con st 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(ma ke_pair("A2", cc_));
cMap_.insert(ma ke_pair("A1", cd_));
cMap_.insert(ma ke_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********@gma il.comwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
On Sep 12, 8:37 pm, digz <Digvijo...@gma il.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<boo st/functional/hash.hpp>
#include<iostr eam>
using namespace std;
struct eqstr
{
bool operator()(cons t 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<con st char*strHashFn;
typedef __gnu_cxx::hash _map<const char*, cacheType, strHashFn,
eqstrcacheMa p;
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(ma ke_pair("A2", cc_));
cMap_.insert(ma ke_pair("A1", cd_));
cMap_.insert(ma ke_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...@gma il.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
4181
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 HashFcn and EqualKey template args) or, 2. convert the two unsigned ints to char*s, concatenate them and use that as Key. For method 1, the difficulty I am having is in writing the HashFcn. HashFcn requires the following method
2
3798
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
3
4047
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. ----------------------------- #include <iostream> #include <fstream>
21
3234
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 (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
2
2344
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
6753
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 = ptr;
21
3917
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 applies in this case though: #include <limits.h> unsigned foo(void) { static const union { unsigned char str;
11
2709
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
11
2712
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 collides with can you spot what I am doing wrong? zork and 122 are to 2 that collide so the "zork" node gets overwritten by 121.
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10778
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10538
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5788
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.