Public Group

Wierd access violation (c++)

This topic is 2780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi
I cannot understand why i get an access violation (unhandled exeption) from my code.

I call

And loadmap function is like this:
void world::loadMap(char * name){ char dest[40]; sprintf(dest,"data/maps/%s",name); FILE * f = fopen(dest,"rb"); if(f){ fileLoad2(city); gui.message("map loaded"); removeExtension(name); sprintf(tempText,"data/maps/%s.jpg",name); if(fileExist(tempText)) gfx.tempPicSetup(&tempTexture,tempSprite,tempText); } else gui.message("ERROR load"); if(f) fclose(f); }

It crashes on removeExtension
void removeExtension( char * string ) { for(int i=0;i<80;i++){ if(string=='.'){ string=0; break; } } }

On the line where i set string=0;
This function works fine in other situations, so i dont know why it crashes on my here...

Plz help, thanks
Erik

Share on other sites
A string literal will give a const char array that can't be changed. That is why you get an exception when you try to change it. Your compiler should warn you about this though.

Share on other sites
1. But sometimes the exact same procedure works fine. How is that possible? It should still be a const char array in those cases.
2. What is the proper way to write the function call (parameters must be changed i guess)

Erik

Share on other sites
1. It is undefined behavior so anything can happen.
2. Calling the function as loadMap("test.map") gives me a warning "warning: deprecated conversion from string constant to char*". Maybe you can turn on some extra warnings to make it easier to detect such mistakes. What you want to do is change the parameter to const char * name and change the function so it works.

Since you are using C++ and not C I think you should look into the C++ Standard Library and use that instead. That will make things much easier and look something like this (untested):std::string removeExtension(const std::string& str) { std::size_t pos = str.find_last_of('.'); if (pos != std::string::npos) { return str.substr(0, pos); } return str; } void world::loadMap(const std::string& name) { std::ifstream f("data/maps/" + name, std::ios::in | std::ios::binary); if (f) { fileLoad2(city); gui.message("map loaded"); std::string tempText = "data/maps/" + removeExtension(name) + ".jpg"; if (fileExist(tempText)) gfx.tempPicSetup(&tempTexture,tempSprite,tempText); } else gui.message("ERROR load"); }

Share on other sites
Writing to that memory is not guaranteed to crash your program. It's just not guaranteed to not crash your program.

You probably don't want to have the input be a editable string anyways. It would be really confusing when a function called loadMap changes the name that you give it.

If you want to stick with the c-syle code, you'd just change loadMap to take a const char* name, and use strcpy to make a new name buffer that you can freely change as you need or desire.

A more C++-style way of doing it would be to use std::string for name, which will do the copy for you.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633363
• Total Posts
3011514
• Who's Online (See full list)

There are no registered users currently online

×