# [c++] Problem with std::string...

## Recommended Posts

Hey, first of all sorry about the poor discription of this topic but I would have no idea how else to describe this problem. My code is as follows:
// -- Header --
// Structure HeightMap
struct HEIGHTMAP { USINT heightData[100*100]; };


// -- Source Main--

// Create TestMap
HEIGHTMAP TestMap;


// -- Source Class LoadMap --
{
// Open mapPath And Load Data Into mapArray
std::ifstream InputFileMap;

InputFileMap.open( mapPath.c_str(), std::ios::in | std::ios::binary );

if (InputFileMap.is_open())
{
InputFileMap.read((char*)(*mapArray).heightData, sizeof(USINT) * 100 * 100);

InputFileMap.close();

this->Logger->Notify("Heightmap Opened Correctly! File: " + mapPath);
} else {
this->Logger->Error("Could Not Open Heigtmap File! File: " + mapPath);
}

return;
}


Now if I debug this in the LoadMap function mapArray has the structure of an std::string. Meening when I look at mapArray in the Watch window I get a structure like this: - mapArray {...} + std::_String_val<char,std::allocator<char> > {...} + _Bx {...} _Myres 0 _Mysize 0 npos 4294967295 The interesting thing is that everything seems to be working fine when I switch the order of arguments in LoadMap like this:
void classMapLoader::LoadMap(HEIGHTMAP* mapArray, std::string mapPath);


Now I'm not sure why this is and any help would be greatly appreciated. If there is any request for more code I'll post it asap. Quinnie

##### Share on other sites
thats kind of messed up,
what compiler + debugger are you using?

##### Share on other sites
Sorry I forgot to mention that, I'm using Visual c++ 2003 .Net

##### Share on other sites
Have you tried cleaning and rebuilding the project?

Are you certain that you don't have two copies of the header file lying around? Like, ANYWHERE on your hard disk?

##### Share on other sites
Cleaning and rebuilding unfortunetly did not help, I'm also quite positive this is the only file with that name on my harddisk.

Also it might help to mention that with an other function I made I have the same problem, again switching the arguments seems to help.

##### Share on other sites
Aside from the weird debugger information, what actually happens when you run the code?

##### Share on other sites
Well, after the read function, where the array should be filled with data from the file, the std::string structure gets filled out with nonsense really. Also I can not acces the elements of mapArray (It is an unsigned short int array from 0 to 100000). When I switch the arguments as mentioned before the array gets filled out nicely and I can access the elements.

##### Share on other sites
Are all of your source files correctly including the header file? I've seen compilers pass the first argument to a function OK, but any other ones get messed up if the function hasn't been prototyped. Just a thought.

##### Share on other sites
Yeah, if the prototype has the arguments the wrong way around, it could cause this sort of problem. The compilation process may not spot the error since the total argument size is identical in both cases.

##### Share on other sites
Quote:
 Original post by f8k8Are all of your source files correctly including the header file? I've seen compilers pass the first argument to a function OK, but any other ones get messed up if the function hasn't been prototyped. Just a thought.

Any C++ compiler should prohibits this (and this is the case for VC++ 2003). A function has to be declared (or defined) before any use (like any other symbol).

##### Share on other sites
I have indeed checked the prototype and it is correct, so that should not be the problem. Well I think I'll just stick to switching the arguments as long as there isnt a better solution. Doesnt really make a difference anyway.

Thanks for all the help though! Godd bless you all.

##### Share on other sites
if you're still interested in getting to the bottom of this, id like you to try out a few things.

1) the first is to manually go into the DEBUG folder and delete the .PDB, .ILK and .PCH files, then rebuild.

2) You might want to also download the Microsoft WinDBG tool for windows (it's official name is "Debugging Tools for Windows"), it's technically a more capable version if the VS debugger, if the problem appears here also then we may be able to eliminate the debugger as the error source.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628290
• Total Posts
2981858

• 11
• 10
• 10
• 11
• 17