|
Error printing default array values from derived class
I have a derived class that contains 2 private arrays ( X, Y) which have default values set using 2 public arrays( aX, aY) using memcpy. To keep things simpler I'll focus on aY. When I try to write these values to the screen using the member function GetYPosition I receive the following output (values 0-9 of aY are correct, but values 10-39 are garbage.
Code:
Beam : 0
Gantry Angle : 0
Collimator Angle : 1
Monitor Units : 5.76
Leaf width : 4
Leaf pairs: 40
Leaf Y position:
-80 i: 0
-76 i: 1
-72 i: 2
-68 i: 3
-64 i: 4
-60 i: 5
-56 i: 6
-52 i: 7
-48 i: 8
-44 i: 9
-842150440 i: 10
-842150451 i: 11
-842150451 i: 12
-842150451 i: 13
-842150451 i: 14
-842150451 i: 15
-842150451 i: 16
-842150451 i: 17
-842150451 i: 18
-842150451 i: 19
-842150451 i: 20
-842150451 i: 21
-842150451 i: 22
-842150451 i: 23
-842150451 i: 24
-842150451 i: 25
-842150451 i: 26
-842150451 i: 27
-842150451 i: 28
-842150451 i: 29
-842150451 i: 30
-842150451 i: 31
-842150451 i: 32
-842150451 i: 33
-842150451 i: 34
-842150451 i: 35
-842150451 i: 36
-842150451 i: 37
-842150451 i: 38
-842150451 i: 39
I don't know whether this is connected but I received an error which I tracked down to the "delete [] pBeam;" line in Field2.cpp
Is there a better to way to initialise these arrays say by defining the arrays within the .cpp file and passing a(nother) pointer to the first element in a SetYPosition member function, since I will need to overwrite the initial values anyway?
Below are the header (Field_2.h) and cpp (Field2.cpp)
// Class definition header file for derived classes FieldSyn (Field_2.h)
// Derived from Field
// FieldSyn has additional private member arrays
// to be initialised
//
#include <iostream>
#include <cstdlib>
using namespace std;
class Field
{
protected:
int LeafPairs;
int LeafWidth;
int GantryAngle;
int CollimatorAngle;
double MonitorUnits;
public:
// constructor
// Field() : LeafPairs(0), GantryAngle(0), CollimatorAngle(0), MonitorUnits(0.0){};
virtual ~Field(){};// Virtual function dynamically determined at runtime (can be overwritten in derived class)
//new constructor for dynamic memory allocation - must be parameterless
Field(){
LeafPairs=GantryAngle=CollimatorAngle=0;
MonitorUnits=0.0;
}
//accessors
void SetGantryAngle (int Gantry) { GantryAngle=Gantry;}
void SetCollimatorAngle (int Colli) { CollimatorAngle=Colli;}
void SetMonitorUnits (double MU) { MonitorUnits=MU;}
int GetLeafPairs () const {return LeafPairs;}
int GetLeafWidth () const {return LeafWidth;}
int GetGantryAngle () const {return GantryAngle;}
int GetCollimatorAngle () const {return CollimatorAngle;}
double GetMonitorUnits () const {return MonitorUnits;}
};
class FieldSyn: public Field
{
private:
int Y[41]; //arrays
int X[80];
public:
//constructor and destructor
// constructor initialises values and arrays
FieldSyn(){
LeafPairs=40;
LeafWidth=4;
/* FieldSyn(){
*/
int aY[]={-80,-76,-72,-68,-64,-60,-56,-52,-48,
-44,-40,-36,-32,-28,-24,-20,-16,-12,
-8,-4,0,4,8,12,16,20,24,28,32,36,34,
40,44,48,52,56,60,64,68,72,76,80};
memcpy(Y, aY, sizeof(Y)/sizeof(int));
int aX[]={110,110,110,110,110,110,110,110,
110,110,110,110,110,110,110,110,
110,110,110,110,110,110,110,110,
50,50,50,50,50,50,50,50,
80,80,80,80,80,80,80,80,
90,90,90,90,90,90,90,90,
30,30,30,30,30,30,30,30,
110,110,110,110,110,110,110,110,
110,110,110,110,110,110,110,110,
110,110,110,110,110,110,110,110};
memcpy(X, aX, sizeof(X)/sizeof(int));
// LeafPairs = 40;
// LeafWidth = 4;
}
//destructor
virtual ~FieldSyn(){};// Virtual function dynamically determined at runtime (can be overwritten in derived class)
//accessors for arrays
void GetYPosition () const {
for(int i=0;i<LeafPairs;i++){
cout << Y[i] << " i: " << i << endl;}}
};
// Field2.cpp
#include <iostream>
#include <cstdlib>
#include"Field_2.h"
using namespace std;
int main()
{
int *NumBeams=0, Beams=1;// max 7 for testing purposes
// define static input arrays for Gantry-, Collimator angle and MUs (testing purposes ONLY)
int GAngle[]={0,52,103,154,206,257,309}, *pGAngle=0; // pointer to array elements
int CAngle[]={1,3,5,7,5,3,1}, *pCAngle=0; // pointer to array elements
double MU[]={5.76,
10.34,
17.36,
25.01,
35.43,
42.17,
55.00}, *pMU=0; //pointer to array elements
// Initialise pointers
pGAngle=&GAngle[0];
pCAngle=&CAngle[0];
pMU=&MU[0];
NumBeams=&Beams;// set ptr NumBeams to address of Beams variable
// Field *pBeam;// Beam[6], *pBeam=0;
FieldSyn *pBeam;
try{
pBeam=new FieldSyn[*NumBeams];
} catch (bad_alloc xa){
cout << "Allocation failure\n";
return 1;
}
for(int i=0;i<Beams;i++){
cout << "\nBeam : " << i << endl;
pBeam->SetGantryAngle(*pGAngle);
cout << "Gantry Angle : " << pBeam->GetGantryAngle() << endl;
pBeam->SetCollimatorAngle(*pCAngle);
cout << "Collimator Angle : " << pBeam->GetCollimatorAngle() << endl;
pBeam->SetMonitorUnits(*pMU);
cout << "Monitor Units : " << pBeam->GetMonitorUnits() << endl;
cout << "Leaf width : " << pBeam->GetLeafWidth() << endl;
cout << "Leaf pairs: " << pBeam->GetLeafPairs() << endl;
cout << "Leaf Y position: " << endl;
pBeam->GetYPosition();
pGAngle++;
pCAngle++;
pMU++;
pBeam++;
}
cout<< endl;
delete [] pBeam; // calls FieldSyn destructor and releases memory (avoid memory "leak")
return 0;
}
|