GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Feb-2009, 05:09
bobsta bobsta is offline
New Member
 
Join Date: Feb 2009
Posts: 5
bobsta is on a distinguished road

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)
CPP / C++ / C Code:
// 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;}}
 
};

CPP / C++ / C Code:
// 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;

}
  #2  
Old 17-Feb-2009, 08:26
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Error printing default array values from derived class


The reason that your array does not get fully copied is this:
CPP / C++ / C Code:
memcpy(Y, aY, sizeof(Y)/sizeof(int));
sizeof(Y) is 164, sizeof(int) is 4. 164/4 = 41. You are only copying 41 bytes of the 164 bytes. (assuming an integer of 4 bytes)
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 16:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 20:23
a tester class and then some. postage Java Forum 1 06-May-2006 16:48
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.