C++ Struct and exceptions - help!

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
EvilBob
Posts: 7
Joined: Thu Oct 15, 2009 10:26 pm

C++ Struct and exceptions - help!

Post by EvilBob »

I'm trying to create a class to handle the "power generating units" in a game, but my C++ knowledge is rubbish and I'm not entirely sure how to approach the error I'm getting.

To explain the structure I'm using; a 'PowerCore' that contains a number of 'sub-units' (cells/batteries/generators/however you prefer to think of them) that can be independantly controlled, but all contribute to the amount of 'power' that the PowerCore can provide.

The 'PowerCore' is represented by a class. Each 'sub-unit' is represented by a struct. When the PowerCore is initialised, it creates an array of those structs.

For some reason there are two particular variables in the struct that cause problems. The functions used to access the problematic lines are also included, I've not included the others. I've tried accessing them directly (line marked *3*) and through the function (lines marked *1* and *2*), same problem.

When they are accessed, it causes an error which may be either;

Code: Select all

Windows has triggered a breakpoint in GameMathsExperiment.exe.
This may be due to a corruption of the heap, which indicates a bug in GameMathsExperiment.exe or any of the DLLs it has loaded.
or

Code: Select all

Unhandled exception at 0x004ffd85 in GameMathsExperiment.exe: 0xC0000005: Access violation writing location 0x00000000. 
What is throwing me is that it only happens with these two particular lines and I don't understand why? Everything else around it works, and all the other floats are handled with identical code.

I've not got that much hair left to risk pulling any more out! I also couldn't say why I chose a struct for this rather than another class, other than it seemed right and I could imagine an array of structs more easily than an array of classes, even though they are very similar beasts.

An abstract of the class is below.

Code: Select all

class cPowerCore{
public:
	struct sUnits{
		float Active;	// The sum of Active, Damaged and Standby
		float Damaged;	// cannot exceed 1.0
		float Standby;	//

		bool Online;	// When offline, Standby = (1.0f - Damaged)
		bool PowerCycling;	//	Whether Unit is changing between Standby and Active;
		float PUEfficiency;	//	How fast cycles up;
		float PDEfficiency;	//	How fast cycles down;
		float RepairEfficiency;	//	How efficient repairs are, between 0.1f and 1.0f - complex systems have lower values

		float Efficiency;	// How much power each unit generates, calc (Active * Efficiency) * Interference to get Volts
		float Interference;	// External factors affecting Efficiency, between 0.1f and 1.0f - more interference has lower value
		float Volts;	// Base for power calc	Calculated (Active * Efficiency) * Interference;
		float Amps;		// Current				Calculated Volts / Ohms
		float Ohms;		// Resistance			Set by initialise, represents imperfections in system
		float Watts;	// Power in system		Calculated Volts * Amps
		float MaxWatts;	// Total Watts if all units Active; Calculated Efficiency * Interference;
	};

	cPowerCore(){}
	~cPowerCore(){}

	void Initialise(int iUnits){
		Unit = new sUnits[iUnits];
		for(int iLoop=0;iLoop<=iUnits;iLoop++){
		        Active(iLoop, 0.0f);
			Damaged(iLoop, 0.0f);
			Standby(iLoop, 0.0f);
			Online(iLoop, false);
			PowerCycling(iLoop, false);
			PUEfficiency(iLoop, 1.0f);
			PDEfficiency(iLoop, 1.0f);
// *1*		RepairEfficiency(iLoop, 1.0f);// CAUSES BREAK POINT?
			Efficiency(iLoop, 1.0f);
// *2*		Interference(iLoop, 1.0f);	// Either of these cause
// *3*		Unit[0].Interference = 1.0f;// an unhandled exception
			Ohms(iLoop, 1.0f);

			};
		}


	void RepairEfficiency(int iUnit, float fRepairEfficiency){ Unit[iUnit].RepairEfficiency = fRepairEfficiency;}
	float RepairEfficiency(int iUnit){return Unit[iUnit].RepairEfficiency;}

	void Interference(int iUnit, float fInterference){ Unit[iUnit].Interference = fInterference;}
	float Interference(int iUnit){return Unit[iUnit].Interference;}

private:
	sUnits * Unit;
};
Edited: Corrected a typo.
Last edited by EvilBob on Sat Oct 24, 2009 5:40 pm, edited 1 time in total.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: C++ Struct and exceptions - help!

Post by randomMesh »

Code: Select all

Unit = new sUnits[iUnits];
for(int iLoop=0;iLoop<=iUnits;iLoop++)
You overflow the heap. Try to use

Code: Select all

for(int iLoop=0;iLoop<iUnits;iLoop++)
instead.

And i see a huge memory leak because you don't delete your units in the destructor.
Last edited by randomMesh on Sat Oct 24, 2009 5:48 pm, edited 1 time in total.
"Whoops..."
EvilBob
Posts: 7
Joined: Thu Oct 15, 2009 10:26 pm

Post by EvilBob »

randomMesh - Yes! Thank you! I completely missed that I was initialising at 0 rather than 1! That's cleared up a number of things.

I do feel quite stupid now though :oops:
Post Reply