std::find, std::end, Exception Error, C++

Started by
18 comments, last by ajm113 15 years, 6 months ago
Hello, I get a exception error on this line:

if(m_mpTextures.find( szFile ) != m_mpTextures.end()){




m_mpTextures And szFile are defined like so:

//In a class
std::map<const char*, NodeTexture*, TextureCompare> m_mpTextures;

//szFile Proccess
char *szFile;
szFile = new char[strlen(szFilename)+1];
	strcpy(szFile, szFilename);

	_strlwr( szFile );

//Then if(m_mpTextures.find( ... and so forth.


Can someone suggest what to do? I am not very good with exceptions that well.. the szFile goes through a process that gives it a value so it isn't NULL. VS2005/C++/Win32
Check out my open source code projects/libraries! My Homepage You may learn something.
Advertisement
Quote:Original post by ajm113
Hello, I get a exception error on this line:


I'm afraid you'll have to tell us exactly what error or exception you get.
That's not going to work like you think it should. char *'s are not strings, so the map will not do a strcmp when it tries to find them (unless you're providing your own comparison function), it will check if the pointer addresses are equal. Use std::strings if you want to use strings as map keys.

I suspect your exception is an access violation because you aren't properly allocating the char*'s you're using as keys. Can we see where in code you do that?
Well I get this exception error:

Unhandled exception at 0x6b94704d (msvcp80d.dll) in main.exe: 0xC0000005: Access violation reading location 0x03010101.

@Driv3MeFar, sorry I am a bit light headed right now, but I don't believe I did that in my code for char *s. Can you give me a small example what you would do?
Check out my open source code projects/libraries! My Homepage You may learn something.
We'd need to see the code for TextureCompare in order to understand what is causing the crash.
However you could probably make the problem go away by just using "std::string" instead of "const char *". That'd make it a whole lot easier too.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by ajm113
Well I get this exception error:

Unhandled exception at 0x6b94704d (msvcp80d.dll) in main.exe: 0xC0000005: Access violation reading location 0x03010101.

@Driv3MeFar, sorry I am a bit light headed right now, but I don't believe I did that in my code for char *s. Can you give me a small example what you would do?


He would just use std::string, and so would I.
struct TextureCompare {	bool operator() (const char* s1, const char* s2) const {	return strcmp(s1, s2) < 0;	}};


Sorry for the late reply, no one was replying a while ago, so I thought this thread was already dead.

But I tried std::strings, but they don't work that well what I been playing with.
Check out my open source code projects/libraries! My Homepage You may learn something.
Quote:Original post by ajm113
Sorry for the late reply, no one was replying a while ago, so I thought this thread was already dead.

But I tried std::strings, but they don't work that well what I been playing with.
You'll find that most of us here disagree. std::strings are what works well, it's char*'s that don't work well. If you think otherwise then you're probably doing it wrong.

Don't forget that people replying here come from all sorts of time zones. As such some people wont get to read other's replies for perhaps at least 24 hours.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Ok, so how do I do this? I already tried changing szFile and making a local string variable out of m_mpTextures, I keep getting errors, can you tell me a correct way on doing this?


Tried #1
std::string TexturesTemp = m_mpTextures;		if(TexturesTemp.find(szFile) != TexturesTemp.end()){



Tried #2
	if(m_mpTextures.find((std::string)szFile) != m_mpTextures.end()){
Check out my open source code projects/libraries! My Homepage You may learn something.
Like this perhaps:
typedef std::map<std::string,NodeTexture*> TextureMap;TextureMap textures;// in some function std::string name = /* whatever */;TextureMap::iterator it = textures.find(name);if(it != textures.end()){    // more code}

Better still to use a smart pointer for the NodeTextures.

This topic is closed to new replies.

Advertisement