Sign in to follow this  
Quinnie

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

Recommended Posts

Quinnie    163
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 A HeightMapLoader Object
classMapLoader MapLoader;

// Create TestMap
HEIGHTMAP TestMap;

// Load TestMap From Heightmap
MapLoader.LoadMap("Data//HeightMaps//Test.raw", &TestMap);


// -- Source Class LoadMap --
void classMapLoader::LoadMap(std::string mapPath, HEIGHTMAP* mapArray)
{
  // 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 this post


Link to post
Share on other sites
Sneftel    1788
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 this post


Link to post
Share on other sites
Quinnie    163
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 this post


Link to post
Share on other sites
Quinnie    163
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 this post


Link to post
Share on other sites
f8k8    171
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 this post


Link to post
Share on other sites
Kylotan    9983
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 this post


Link to post
Share on other sites
Quote:
Original post by f8k8
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.


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 this post


Link to post
Share on other sites
Quinnie    163
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 this post


Link to post
Share on other sites
silvermace    634
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.

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