app crash when there's more than 3000 entries in std::map ?

Hi everyone

I have this strange behaviour happening with this code which I can't
explain. On my computer when I set nt with a value greater than 3000
it crashes. Is there a max number of keys I can use with a std::map ?

thanks a lot

#include <vector>
#include <iostream>
#include <map>

#include <string>


int main()
{
	float t = clock();
	int nt = 3000;
#if 1
	hash_map<std::string, int, hash<std::string> > mymap;
	for ( int i = 0; i < nt; ++i )
	{
		string tmp = "test" + i;
		mymap[ tmp ] = i;
	}
	for ( int i = 0; i < 10e5; ++i )
	{
		int a = (int)(drand48() * nt);
		string tmp = "test" + a;
		hash_map<std::string, int, hash<std::string> >::iterator it =
mymap.find( tmp );
		if ( it != mymap.end() )
		{
		}
		else
		{
			printf("not found\n");
		}
	}
	unsigned max_size = mymap.max_size();
	printf("%d\n", max_size );

#else
	std::map<std::string, int> mymap;
	for ( int i = 0; i < nt; ++i )
	{
		string tmp = "test" + i;
		mymap[ tmp ] = i;
	}
	for ( int i = 0; i < 10e5; ++i )
	{
		int a = (int)(drand48() * nt);
		string tmp = "test" + a;
		std::map<std::string, int>::iterator it = mymap.find( tmp );
		if ( it != mymap.end() )
		{
		}
		else
		{
			printf("not found\n");
		}
	}

	unsigned max_size = mymap.max_size();
	printf("%d\n", max_size );

#endif
	printf("time %f\n", (clock() - t ) / float( CLOCKS_PER_SEC ) );

	return 0;
}

#endif
0
mast4as (90)
8/12/2010 5:33:22 PM
comp.lang.c++ 49094 articles. 0 followers. Post Follow

4 Replies
251 Views

Similar Articles

[PageSpeed] 27

Sorry you need to add

#include <ext/hash_map>
using namespace __gnu_cxx;
namespace __gnu_cxx {

// hash specialisation to allow hashing of strings
template<>
struct hash<std::string>
{
	size_t operator()(const std::string &__s) const { return
__stl_hash_string(__s.c_str()); }
};

} // namespace __gnu_cxx OR std

at the top if you want this to compile. But hash_map and map behaves
the same

using c++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
On Aug 12, 6:33=A0pm, mast4as <mast...@yahoo.com> wrote:
> Hi everyone
>
> I have this strange behaviour happening with this code which I can't
> explain. On my computer when I set nt with a value greater than 3000
> it crashes. Is there a max number of keys I can use with a std::map ?
>
> thanks a lot
>
> #include <vector>
> #include <iostream>
> #include <map>
>
> #include <string>
>
> int main()
> {
> =A0 =A0 =A0 =A0 float t =3D clock();
> =A0 =A0 =A0 =A0 int nt =3D 3000;
> #if 1
> =A0 =A0 =A0 =A0 hash_map<std::string, int, hash<std::string> > mymap;
> =A0 =A0 =A0 =A0 for ( int i =3D 0; i < nt; ++i )
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 string tmp =3D "test" + i;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 mymap[ tmp ] =3D i;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 for ( int i =3D 0; i < 10e5; ++i )
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int a =3D (int)(drand48() * nt);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 string tmp =3D "test" + a;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 hash_map<std::string, int, hash<std::stri=
ng> >::iterator it =3D
> mymap.find( tmp );
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if ( it !=3D mymap.end() )
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("not found\n");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 unsigned max_size =3D mymap.max_size();
> =A0 =A0 =A0 =A0 printf("%d\n", max_size );
>
> #else
> =A0 =A0 =A0 =A0 std::map<std::string, int> mymap;
> =A0 =A0 =A0 =A0 for ( int i =3D 0; i < nt; ++i )
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 string tmp =3D "test" + i;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 mymap[ tmp ] =3D i;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 for ( int i =3D 0; i < 10e5; ++i )
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int a =3D (int)(drand48() * nt);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 string tmp =3D "test" + a;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 std::map<std::string, int>::iterator it =
=3D mymap.find( tmp );
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if ( it !=3D mymap.end() )
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("not found\n");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 unsigned max_size =3D mymap.max_size();
> =A0 =A0 =A0 =A0 printf("%d\n", max_size );
>
> #endif
> =A0 =A0 =A0 =A0 printf("time %f\n", (clock() - t ) / float( CLOCKS_PER_SE=
C ) );
>
> =A0 =A0 =A0 =A0 return 0;
>
> }
>
> #endif

0
mast4as (90)
8/12/2010 5:34:57 PM
On Aug 12, 12:33=A0pm, mast4as <mast...@yahoo.com> wrote:
> Hi everyone
>
> I have this strange behaviour happening with this code which I can't
> explain. On my computer when I set nt with a value greater than 3000
> it crashes. Is there a max number of keys I can use with a std::map ?
>

It's because this doesn't do what you think it does:

> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 string tmp =3D "test" + i;

It does *not* make a string named "test1", "test2", etc.
Instead, it does pointer arithmetic on the const char*
that points to "test".

So
   ("test" + 0) is a pointer to the string "test"
   ("test" + 1) is a pointer to the string "est"
   ("test" + 2) is a pointer to the string "st"

etc.

Once i passes the null byte in the string literal, though,
you're into some random chunk of memory. All bets are off.
Eventually you're reading outside of memory that belongs
to your application and you get a segfault.

--Jonathan
0
8/12/2010 5:49:01 PM
On Aug 12, 6:49=A0pm, Jonathan Lee <jonathan.lee....@gmail.com> wrote:
> On Aug 12, 12:33=A0pm, mast4as <mast...@yahoo.com> wrote:
>
> > Hi everyone
>
> > I have this strange behaviour happening with this code which I can't
> > explain. On my computer when I set nt with a value greater than 3000
> > it crashes. Is there a max number of keys I can use with a std::map ?
>
> It's because this doesn't do what you think it does:
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 string tmp =3D "test" + i;
>
> It does *not* make a string named "test1", "test2", etc.
> Instead, it does pointer arithmetic on the const char*
> that points to "test".
>
> So
> =A0 =A0("test" + 0) is a pointer to the string "test"
> =A0 =A0("test" + 1) is a pointer to the string "est"
> =A0 =A0("test" + 2) is a pointer to the string "st"
>
> etc.
>
> Once i passes the null byte in the string literal, though,
> you're into some random chunk of memory. All bets are off.
> Eventually you're reading outside of memory that belongs
> to your application and you get a segfault.
>
> --Jonathan

Thanks a lot yes ... I just figured (long day at work ;-(

Thank you so much
std::string createRandomString()
{
	char alpha[ 6 ] =3D { 'a', 'b', 'c', 'd', 'e', 'f' };
	char str[ 10 ];
	for ( int i =3D 0 ; i < 10 ; i++ )
	{
		int a =3D int(drand48()*5);
		str[ i ] =3D a;
	}
	return std::string( str );
}

int main()
{
	float t =3D clock();
	int nt =3D 10000;
#if 1
	hash_map<std::string, int, hash<std::string> > mymap;
	for ( int i =3D 0; i < nt; ++i )
	{
		string tmp =3D createRandomString();
		mymap[ tmp ] =3D i;
	}
	for ( int i =3D 0; i < 10e5; ++i )
	{
		int a =3D (int)(drand48() * nt);
		string tmp =3D createRandomString();
		hash_map<std::string, int, hash<std::string> >::iterator it =3D
mymap.find( tmp );
		if ( it !=3D mymap.end() )
		{
		}
		else
		{
			//printf("not found\n");
		}
	}
	unsigned max_size =3D mymap.max_size();
	printf("%d\n", max_size );

#else
	std::map<std::string, int> mymap;
	for ( int i =3D 0; i < nt; ++i )
	{
		string tmp =3D createRandomString();
		mymap[ tmp ] =3D i;
	}
	for ( int i =3D 0; i < 10e5; ++i )
	{
		int a =3D (int)(drand48() * nt);
		string tmp =3D createRandomString();
		std::map<std::string, int>::iterator it =3D mymap.find( tmp );
		if ( it !=3D mymap.end() )
		{
		}
		else
		{
			//printf("not found\n");
		}
	}

	unsigned max_size =3D mymap.max_size();
	printf("%d\n", max_size );

#endif
	printf("time %f\n", (clock() - t ) / float( CLOCKS_PER_SEC ) );

	return 0;
}

#endif
0
mast4as (90)
8/12/2010 5:56:03 PM
On Aug 12, 12:56=A0pm, mast4as <mast...@yahoo.com> wrote:
> Thanks a lot yes ... I just figured (long day at work ;-(
>
> Thank you so much
> std::string createRandomString()
> {
> =A0 =A0 =A0 =A0 char alpha[ 6 ] =3D { 'a', 'b', 'c', 'd', 'e', 'f' };
> =A0 =A0 =A0 =A0 char str[ 10 ];
> =A0 =A0 =A0 =A0 for ( int i =3D 0 ; i < 10 ; i++ )
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int a =3D int(drand48()*5);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 str[ i ] =3D a;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 return std::string( str );
>
> }

No problem. BTW, if you want to avoid these random strings
you could probably use next_permutation from <algorithm>
to get some string names. Make a char array of distinct
letters, permute them, and use string::append to get your
dummy string.

(Not entirely related, just something I've found useful
before)

--Jonathan
0
8/12/2010 6:03:14 PM
Reply:

Similar Artilces:

Is std::map, how to use for_each to execute the map's second's memfun?
Following are my code; struct MapTest { CMapTest(int nx = 0) { x = nx; } protected: int x; public: int PrintX() { count<<x<<endl; } }; void main() { std::map<int, CMapTest*> test; for (int i = 0; i < 10; ++i) test.insert(std::make_pair(i, new CMapTest(i * 100))); // hot to use for_each to replace the for process? for(std::map<int, CMapTest*>::iterator iter = test.begin(); iter != test.end(); iter++) { (*iter).second->PrintX(); } } [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderat...

Trial of Musatov's theory: double spaced 'e x p a n d s' and single spaced 'c o n t r a c t s'. Does the spacing post-processing appear equal? If so, what force was lost and where did it g
Musatov's theory 'The space between spaces expands and contracts.' Would this give time a direction? Musatov ...

It's on-it's off-it's on, it's off -WHY?
IS THIS THE CORRECT FORUM? Hope so. We have three computer networked at home...all with Windows XP. one of those is a laptop. Some mornings all 3 can see each other and transfer files...then mysteriously one or more will disappear. No contact....then later it's back. The laptop is the worst. Most often it won't let us in...but sometimes it will. No obvious reason. The most INFURIATING part is the message "see your administrator". Dammit Bill Gates...don't you realize that most networks are small offices without a Majestic Royal Administrator sitting his i...

Fans rap Apple's 'crap' Map app
I do think that the fickle Apple Fans are an ungrateful lot. They've just got their brand new 1OS 6, complete with freedom *not* to be able to use Google maps, and all they can do is complain! Worthless wretches - obviously ought to be expelled from the Apple barrel. Just look at some of the trivia that these folks complain about, after all Apple's hard work:- <quote> Apple today faced the ire of thousands of irritated iOS 6 users who upgraded to the new version of the iDevice operating system only to discover the Cupertino's new Maps app is, well, pants....

Referencing an Image's Map's Link's Details
Hi All, I have scoured my JavaScript book, but can't find the answer, so I hope that you can help... I have a banner on my webpage, called (creatively) "banner". This banner uses a map called "bannerMap". The map has an area called "homeLink". The area's href is "home.htm". How do I reference this href? I have tried: document.bannerMap.homeLink.href - FAILED document.banner.bannerMap.homeLink.href - FAILED document.homeLink.href - FAILED document.banner.homeLink.href - FAILED Has anyone tried this before? Is it possible? What is the answer?!...

More factory sealed FPGA's with C of C's
I have more FPGA's with C of C's that I can supply below facotry direct pricing. Small quantities ok. These have all been purchased thru factrory direct channels and in most cases I can tell you exactly where they came from. We have aquired these from VMI and SMI programs that we have with major OEM's and CM's. Jon E. Hansen Pyramid Technologies Inc. jon@pyramidemail.com www.pyramidtechnologiesinc.com (949)864-7745 Direct (949)636-1416 Cell CERTIFIED ISO 9001:2000 XC5VFX70T-1FF1136C XC5VFX70T-1FFG1136C XC5VLX50-1FF676C XC5VLX330-1FF1760C XC4VLX100-10FFG1148C XC2V3000-4FG...

SGI's 'make' strips -LANG:std from the link line??
Hello I'm trying to use autoconf to generate makefiles for a combination C, C++, and Fortran program. The C and Fortran parts are built into static libraries which are then supposed to link with the C++ object files to create the executable. The problem is that using autoconf's 'CXXFLAGS' variable to pass '-LANG:std' to SGI's 'make' doesn't work. 'make' is stripping -LANG:std from the link line! It works fine if I paste it into a terminal window command; but if it's in the Makefile make removes it. Any thoughts on how to g...

Implementing GO4's Observer pattern for std::stirng and std::vector/std::map
Hi, I want to "observe" changes to std::string and std::vector/std::map structures. The idea being that whenever code modifies a string, or adds/deletes a collection element, my code is notified so that I can invalidate view objects, etc. I know that I can wrap these data structures within my own classes and add the observer hooks to my accessor routines. This is a tedious and error-prone process for things like std::string with lots of methods. I'm curious if others have found a good way to do this for STL strings and containers. All my reading warns against su...

The "Tipping Point" for app's, ALL app's ?
tipping point - ************** noun The point at which a series of small ineffective changes acquires enough pressure or importance to cause a larger, more significant change. The point at which the buildup of minor incidents reaches a level that causes someone to do something they had formerly resisted. ---------------------------------------------------- Lots of Mac users, myself included, are impatiently waiting for the "tipping point" to be reached in lots of app's. ("MacSpeech Dictate" in particular) The point where the app's are _immediately...

What's the C++ community's feeling about lambda(or closure) in C++?
I know this topic sounds kind of old and dry(and shallow, maybe :)), but, you see, lately I found that there's all the fuss about closure in Java, so I poked around for a little, and did find some valuable information. And I started to remember that there were a proposal(actually two) about lambda/closure for C++(see http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2006/n1968.pdf and http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2006/n1958.pdf) They're all pretty well-formed and sound, though I found Bjarne's a little more comprehensive. It simply seems that the closure id...

Free math' app' threatens commercial math' app's
For Mac math' buffs, a new FREE open-source math' app' called SAGE will be available for free shortly, which claims to beat expensive commercial software like Mathematica which costs $2,500 Some team of math' experts at University of Washington are developing the app' Supposedly SAGE has already won awards in open competition at some world-wide math' conference. <http://uwnews.washington.edu/ni/article.asp?articleID=38459> Mark- In article <noneof-AC775C.02292008122007@earthlink.vsrv-sjc.supernews.net>, Mark Conrad <noneof@urbusiness.invalid...

How to link obj's from c-source with obj's from c++-source
Hi, I compiled 2 simple source-files with cl -c main.cpp and cl -c test.c. test.c contains just a simple function, which is declared in test.h. test.h is included in test.c and in main.cpp. When I try to link the objects with link main.cpp test.c -out:prog.exe I get the error: "main.obj : error LNK2019: unresolved external Symbol "int __cdecl greater(int,int)" (?greater@@YAHHH@Z), called in function _main prog.exe : fatal error LNK1120: 1 unresolved external Symbol" What have I to do, to be able to link those objects together? Guido Belligoi "Guido Belligoi" ...

Any XL C/C++'s option to determine the compiler's predefined macros
Hi, AIX 5.3 $ xlc++ -qversion IBM XL C/C++ Enterprise Edition V8.0 for AIX Version: 08.00.0000.0014 Is there any XL C/C++'s option to determine the compiler's predefined macros? Alex Vinokur In article <2e672e57-f577-4134-9e81-03eb6b3097b7@35g2000pry.googlegroups.com>, Alex Vinokur <alexvn@users.sourceforge.net> writes: > Hi, > > AIX 5.3 > > $ xlc++ -qversion > IBM XL C/C++ Enterprise Edition V8.0 for AIX > Version: 08.00.0000.0014 > > Is there any XL C/C++'s option to determine the compiler's predefined > macros? > > Ale...

Q: C macro's for lvalue statements ? Any C marco Guru's out there ?
Hi everybody, I've got a problem implementing some macros that would make my code much more readable. The idea: I have a set of macros that create variable names depending of the content of some other define. An example (of how it should look like in the end): // --------------- cut #define BASE_NAME module1 // the macro(s) which I need help for :-) #define MAKE_VARNAME2(base, var,val) int var#_#base = val #define MAKE_VARNAME(var, val) MAKE_VARNAME2(BASE_NAME, var, val) // the usage example MAKE_VARNAME(status, 0); // --------------- cut which should expand to int status_module1...

Web resources about - app crash when there's more than 3000 entries in std::map ? - comp.lang.c++

Resources last updated: 2/4/2016 6:08:59 AM