I am using codeblocks and I am getting this error:
Program received signal (SIGTRAP)
Trace/breakpoint trap
Previous frame inner to this frame (corrupt stack?)
error
What does it mean? I have searched google and thus far have gotten zilch?
I am using codeblocks and I am getting this error:
Program received signal (SIGTRAP)
Trace/breakpoint trap
Previous frame inner to this frame (corrupt stack?)
error
What does it mean? I have searched google and thus far have gotten zilch?
Here is the function where the error occurs
In debug mode the program gets all the way to std::cout << "File Closed\n";Code:#include "CLoadLevel.h" void Pause(); CLoadLevel::CLoadLevel() { }; CLoadLevel::~CLoadLevel() { }; void CLoadLevel::LoadLevel(CCharacter* enemy, CItem* weapon, char* filename) { std::cout << "Enter Function Load Level\n" << std::endl; Pause(); std::ifstream in(filename, std::ios::binary); std::cout << "Reading in enemycount\n" << std:: endl; Pause(); in.read((char*)&enemycount, sizeof(int)); std::cout << "enemycount = " << enemycount << std::endl << std::endl; enemy[enemycount]; for(int count = 0; count < enemycount; count++) { std::cout << "\nReading in enemy strength\n" << std::endl; int strength = 0; in.read((char*)&strength, sizeof(int)); std::cout << "Strength read = " << strength << std::endl << std::endl; Pause(); enemy[count].SetStrength(strength); int dexterity = 0; in.read((char*)&dexterity, sizeof(int)); enemy[count].SetDexterity(dexterity); int intelligence = 0; in.read((char*)&intelligence, sizeof(int)); enemy[count].SetIntelligence(intelligence); int endurance = 0; in.read((char*)&endurance, sizeof(int)); enemy[count].SetEndurance(endurance); int hp = 0; in.read((char*)&hp, sizeof(int)); enemy[count].SetHP(hp); int maxhp = 0; in.read((char*)&maxhp, sizeof(int)); enemy[count].SetMaxHP(maxhp); int mp = 0; in.read((char*)&mp, sizeof(int)); enemy[count].SetMP(mp); int maxmp = 0; in.read((char*)&maxmp, sizeof(int)); enemy[count].SetMaxMP(maxmp); int gold = 0; in.read((char*)&gold, sizeof(int)); enemy[count].SetGold(gold); std::cout << "XP" << std::endl; int xp = 0; in.read((char*)&xp, sizeof(int)); enemy[count].SetXP(xp); } in.read((char*)&weaponcount, sizeof(int)); weapon[weaponcount]; for (int count = 0; count < weaponcount; count++) { int mindamage = 0; in.read((char*)&mindamage, sizeof(int)); weapon[count].SetMinAmount(mindamage); std::cout << "minDamage " << mindamage << std::endl; int maxdamage = 0; in.read((char*)&maxdamage, sizeof(int)); weapon[count].SetMaxAmount(maxdamage); int price = 0; in.read((char*)&price, sizeof(int)); weapon[count].SetPrice(price); std::cout << "Price " << price << std::endl; } std::cout << "Closing file\n"; in.close(); std::cout << "File Closed.\n"; };
before it crashes and I get my SIGTRAP error. Is it maybe cuz I ran out of memory...my program is on a 3.5" floppy.
> enemy[enemycount];
What's this supposed to do?
To me, it just looks like an out of bound memory reference.
Is enemy even allocated any space when you call the load function, and if so, how much?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I read in the variable enemycount here> enemy[enemycount];
What's this supposed to do?
To me, it just looks like an out of bound memory reference.
Is enemy even allocated any space when you call the load function, and if so, how much?
Code:std::cout << "Reading in enemycount\n" << std:: endl; Pause(); in.read((char*)&enemycount, sizeof(int)); std::cout << "enemycount = " << enemycount << std::endl << std::endl;
Tho now I just noticed that I forgot to declare enemycount...Cant believe I missed that.
Shouldn't that have given me a compile error?
I see my problem...I inialized my object arrays to hold only three objectsCode:int main() { CEnemy* newenemy = new CEnemy[3]; CWeapon* neww = new CWeapon[3]; CLoadLevel load; load.LoadLevel(newenemy, neww, "LevelTest.dat"); std::cout << "Exited LoadLevel Function\n" << std::endl; delete [] newenemy; delete [] neww; Pause(); return 0; }
Last edited by eaane74; 11-19-2007 at 11:31 AM.
You should use vectors instead of dynamic arrays. They are easier, safer and more flexible. If you did, then you could use push_back to add to the vector instead of worrying about what size to create the dynamic arrays before calling the function.
>> I just noticed that I forgot to declare enemycount... Shouldn't that have given me a compile error?
Perhaps it is a member variable of CLoadLevel?
You are right.>> I just noticed that I forgot to declare enemycount... Shouldn't that have given me a compile error?
Perhaps it is a member variable of CLoadLevel?
Ill try using vectors...should've done it before.
THANKS Daved...It seems to work fine.
Now I know why you guys stress using vectors so much over dynamic arrays!
> CEnemy* newenemy = new CEnemy[3];
Well if enemycount is >= 3, then the code is broken as it trashes memory you don't own.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
That was poor coding on my part. I dont think I will be using arrays anymore> CEnemy* newenemy = new CEnemy[3];
Well if enemycount is >= 3, then the code is broken as it trashes memory you don't own