Sign in to follow this  
azedor

map

Recommended Posts

Look at this code below: char buf[80]; struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; } }; struct symbol_t { char name[80]; }; map<const char*, symbol_t *, ltstr> symbols; void Add_Symbol(const char *name) { symbol_t *s = new symbol_t; strcpy(s->name, name); symbols[s->name] = s; // it has to be wrong, dunno why !!! } This is simplified version of my code, but i hope sufficient to show the problem. Generally speaking problem is that keys in map sometimes are overriten. Let's say that I execute sth like this: Add_Symbol(buf); // buf has value = "key1"; ...here code which changes content of buf to value "key2" ...here is possible that in my map value of the inserted key will change alsoe from "key1" to "key2" And its very weird for me because content of the struct under this key won't never changes, and as you noticed value of the key comes from struct. Can you tell me how to resolve this problem ? Necesssary for me is to use key of type "const char*" instead of "string".

Share this post


Link to post
Share on other sites
Quote:
Original post by azedor
Can you tell me how to resolve this problem ? Necesssary for me is to use key of type "const char*" instead of "string".


So you know the solution...

Share this post


Link to post
Share on other sites
Well, first of all if you are using C++, favor strings over char arrays. Suddenly life becomes easier (look, ma, no memory leaks!):

std::map<const std::string, std::string> symbols;

void Add_Symbol(const std::string symbol){
symbols[symbol] = symbol;
}

I can't really think of any particular reason for "needing" to use a const char* unless this is a homework assignment.

*Edit: That said, after looking and not seeing an actual compile error I actually tried it and it works which means you are likely having an error elsewhere.

[Edited by - M2tM on February 24, 2010 2:31:43 PM]

Share this post


Link to post
Share on other sites
Quote:

This is simplified version of my code, but i hope sufficient to show the problem.

Your code, as posted, should work.

However, because you are using pointers into the symbols as keys, you cannot modify the symbol names once they are created, otherwise the behaviour of the map won't be defined.

Is there a reason you cannot use std::string?

Share this post


Link to post
Share on other sites
Quote:
Original post by azedor
Can you tell me how to resolve this problem ? Necesssary for me is to use key of type "const char*" instead of "string".

std::string has a member function, c_str(), which gives you a const char*, for when you need that.

As for your problem, you're either changing the name of one of those symbol_t instances, or there's some memory corruption going on. Can you show a minimum example that still contains the bug? The code you've posted is too incomplete and most likely doesn't contain the bug that you're looking for.

Peculiar mix of C and C++ you've got there bytheway...

Share this post


Link to post
Share on other sites
http://codeviewer.org/view/code:c59 - here is the entire code, i marked with comments places when most probable i'm doing sth wrong, and explained what exactly happens at this point. Code uses external variable char yytext[];

You can omit this parts of code which i didn't marked with comments, maybe you will see any serious mistake. I know what happens, and when (marked crucial function) but i don't know why, because functions seems to work correctly during first few calls.

Share this post


Link to post
Share on other sites
I don't expect anyone to understand what this code is supposed to do just hope that I made clear mistake of any kind which is visible at first glance for experienced coders.

This program is somewhat lab assignment and i can not use strings.

Share this post


Link to post
Share on other sites
I found mistake in my code, It was fundamental as I thought.

In order to find element in a map under a given key I made sth like this:
if (mymap[key] != NULL) ...//
"key" was a global variable, constantly changing, I didn't even consider the case that this instruction would actually put new element in the map. So it was bloody difficult to me to track down this mistake.
Even changing const char* to strings wouldn't help me in this case.
Now I know how to do it right and everything works as I expect.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this