only on DEBUG this code crashes

Started by
12 comments, last by EvilNando 12 years, 9 months ago

int main()
{
ifstream _file_input("GameData.dat", ios::binary);
for (int _idx = 0; _idx < 3; _idx++)
{
//cout<<"size of Monster "<<sizeof(GameMonster)<<" ";
//cout<<"current seek pointer position :"<<_file_input.tellg()<<endl;

//_file_input.seekg(_idx * sizeof(GameMonster));
_file_input.read((char*)&_monster, sizeof(GameMonster));

cout<<_monster.Damage<<" "<<_monster.Hp<<" "<<_monster.Name<<endl;
}

_file_input.close();

cout<<"\nPress a key"<<endl;
getchar();
// PAST THIS LINE THE PROGRAM CHRASHES
}


the error
Unhandled exception at 0x581fad4a (msvcp100d.dll)
0xC0000005: Access violation reading location 0x00a94be4

if I switch to release the code runs just fine, pretty weird huh?
Advertisement
It's not weird, it's common of bugs relating to uninitialized memory, or memory corruption -- the debug build includes extra checks to try and make bad code crash, so that you realize that you've got some kind of corruption going on.

Where/what is [font="Lucida Console"]_monster[/font]?
Is _monster really an object of class/struct GameMonster? And what type are Damage, Hp and Name members of _monster object?
Are there some other global variables with non-trivial constructor or destructor in your program?
Whether or not it's weird depends on what your GameMonster type looks like. If it's non-POD, then it's not weird at all.
heres the full soruce

maybe something Im missing?




#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>

using namespace std;

class GameMonster
{
public:
string Name;
int Hp;
int Damage;

GameMonster() { Name = ""; Hp = 0; Damage = 0; }
GameMonster(string name, int hp, int dmg) { Name = name; Hp = hp; Damage = dmg; }
};


class GameNpc
{
public:
string Name;
int Hp;
int Damage;
string SpecialAttack;

GameNpc() { Name = ""; Hp = 0; Damage = 0; SpecialAttack = ""; }
GameNpc(string name, int hp, int dmg, string specialAtk) { Name = name; Hp = hp; Damage = dmg; SpecialAttack = specialAtk; }
};



void main()
{
GameMonster _monster;
GameMonster _monsters[3] = { GameMonster("Troll", 50, 20), GameMonster("Skeleton", 10, 5), GameMonster("Rat", 5, 1) };
GameNpc _npcs[3] = { GameNpc("Gawein", 100, 25, "Smity"), GameNpc("Vivi", 25, 5, "LulzSkillz"), GameNpc("Necro", 40, 2, "Raise Dead") };

ofstream _file("GameData.dat", ios::binary);

_file.write((char*)_monsters, sizeof(_monsters));

_file.seekp(0, ios::end);
cout<<"size of GameData.dat "<<_file.tellp()<<" bytes"<<endl<<endl;

_file.close();



ifstream _file_input("GameData.dat", ios::binary);
for (int _idx = 0; _idx < 3; _idx++)
{
cout<<"size of Monster "<<sizeof(GameMonster)<<" ";
cout<<"current seek pointer position :"<<_file_input.tellg()<<endl;

//_file_input.seekg(_idx * sizeof(GameMonster));
_file_input.read((char*)&_monster, sizeof(GameMonster));

cout<<_monster.Damage<<" "<<_monster.Hp<<" "<<_monster.Name<<endl;
}

_file_input.close();

cout<<"\nPress a key"<<endl;
getchar();
}

std::string is non-POD, and since your classes contain it, your classes are non-POD. This means that you can't just copy the bits that make it up to a file and read it back. You need to do actual serialization.
I dont understand what POD means, but I guess I should use fixed width char arrays?
The first link when I put "POD C++" into google.
The links says nothing about how to fix the problem so Ill ask again

should I use fixed char arrays instead or is there an actual way of reading std::strings of variable size using ifstream

<div><br></div><div><br></div><div>edit: I just tried using very long strings just to test what happened if I varied each of the monsters actual byte size but it still working properly (without considering the crash at the end of program)</div>
Does it really prints out string as it is in file? Even for long strings? (>30 chars)

This topic is closed to new replies.

Advertisement