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.
Code: Select all
Unhandled exception at 0x004ffd85 in GameMathsExperiment.exe: 0xC0000005: Access violation writing location 0x00000000.
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;
};