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 19-Feb-2009, 08:18
bobsta bobsta is offline
New Member
 
Join Date: Feb 2009
Posts: 5
bobsta is on a distinguished road

Error in execution of 2D dynamic array


I am new to C++ (6 weeks and counting) and am using VC++ 6.0 and receive the attached error (see attached file) when executing my code. I am creating a dynamic 2D array of derived objects, which are used to simulate a medical linear accelerator for those who may be interested.
I am fairly certain it lies around the two delete lines (lines 115-116) at the end of the .cpp file. However as a newbee I am not sure what I have done wrong (apologies for the inelegant coding)! Thanks for taking the time to read this.

CPP / C++ / C Code:
// Class defintion header file for class Field and derived classes FieldSyn (Field_2.h)
// FieldSyn has additional private member arrays (X,Y)
// to be initialised via public arrays aY and aX
// Arrays are now initialised by passing appropriate pointer address and looping
// 

#include <iostream>
#include <cstdlib>


using namespace std;

class Field
{
protected:
    int LeafPairs;
    int LeafWidth;
    int GantryAngle;
    int CollimatorAngle;
    double MonitorUnits;
public:
    // constructor
Field(){
		LeafPairs=GantryAngle=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
	

    //accessors/member functions
    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 X[80], Y[41]; 
public:
    //constructor and destructor
    // constructor initialises values and arrays
	FieldSyn(){
		LeafPairs=40;
		LeafWidth=4;
		
    }
	int aX[80], aY[41]; // public arrays for Leaf position (aX) and Leaf boundary (aY)
    //destructor
    virtual ~FieldSyn(){};// Virtual function dynamically determined at runtime (can be overwritten in derived class)

	//accessors for arrays
	void SetYPosition (int *pY) {
		for (int i=0;i<LeafPairs+1;i++){
			aY[i]=*pY;
			pY++;//advance pointer
		}
	pY=pY-(LeafPairs+1);// reset pointer
	}
	void SetXPosition (int *pX) {
		for (int i=0;i<2*LeafPairs;i++){
			aX[i]=*pX;

			pX++;//advance pointer
		}
	pX=pX-(2*LeafPairs);// reset pointer
	}

	void GetYPosition () const {
		for(int i=0;i<LeafPairs+1;i++){
			cout << aY[i] << "\\";}
		cout <<endl;}

	void GetXPosition () const {
		for(int i=0;i<2*LeafPairs;i++){
			cout <<  aX[i] << "\\";}
		cout << endl;}

	
};

CPP / C++ / C Code:
// Class defintion for simple derived class FieldSyn
// Derived from Field
// FieldSyn has additional private member arrays
// to be initialised
// Modified to "dynamically" create instances of Field objects at runtime
// In final program value of Beams will be read in from DICOM RT PLAN file
// 

#include <iostream>
#include <cstdlib>
#include"Field_2.h"

using namespace std;


int main()
{

	int *NumBeams=0, Beams=9;// max 9 for testing purposes
	int *NumSegm=0, Segments=3; // needed for 2D dynamic array
	
	// define static input arrays for Gantry-, Collimator angle and MUs (testing purposes ONLY)

	int GAngle[]={0,20,40,80,120,240,280,320,340}, *pGAngle=0; // pointer to array elements
	int CAngle[]={1,3,5,7,5,3,1,3,5}, *pCAngle=0; // pointer to array elements
	double MU[]={5.76,
				10.34,
				17.36,
				25.01,
				35.43,
				42.17,
				47.32,
	            49.27,
				55.00}, *pMU=0; 
	int YPos[] ={-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,
				40,44,48,52,56,60,64,68,72,76,80}, *pY=0; // Synergy MLC
	int XPos[] ={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},*pX;//pointer to array elements Synergy

//  Initialise pointers
	pX=&XPos[0];
	pY=&YPos[0];
	pGAngle=&GAngle[0];
	pCAngle=&CAngle[0];
	pMU=&MU[0];
	NumBeams=&Beams;
	NumSegm=&Segments;// set ptr NumBeams to address of Beams variable


	FieldSyn **pBeam = new FieldSyn*[Beams]; // rows=Beams : dynamically allocate 2D array of FieldSyn objects
	for (int j=0;j<Beams;j++){
		pBeam[j]=new FieldSyn[Segments]; // column of Beam objects: Segments

/*	try{
		pBeam=new FieldSyn[*NumBeams];
	} catch (bad_alloc xa){
		cout << "Allocation failure\n";
		return 1;
	}
/*  Loop over all Beams to set gantry and collimator angle, MUs etc.
*/  cout << "\nBeam : " << j << endl;
    for(int i=0;i<Segments;i++){
	
	
	cout << "\nSegment: " << i << endl;

	pBeam[j]->SetGantryAngle(*pGAngle);
    cout << "Gantry Angle : " << pBeam[j]->GetGantryAngle() << endl;

    pBeam[j]->SetCollimatorAngle(*pCAngle);
    cout << "Collimator Angle : " << pBeam[j]->GetCollimatorAngle() << endl;

    pBeam[j]->SetMonitorUnits(*pMU);
    cout << "Monitor Units : " << pBeam[j]->GetMonitorUnits() << endl;

	cout << "Leaf width : " << pBeam[j]->GetLeafWidth() << endl;
	cout << "Leaf pairs: " << pBeam[j]->GetLeafPairs() << endl;

	pBeam[j]->SetYPosition(pY); // try to initialise values with accessor
	cout << "Leaf Y position: " << endl;
	pBeam[j]->GetYPosition(); // return Y positions
	
	pBeam[j]->SetXPosition(pX);
	cout << "Leaf X position: " << endl;
	pBeam[j]->GetXPosition();
//  Advance pointers
//	pGAngle++;
//	pCAngle++; 
	pMU++;
	
	}
	// reset pointers
	pGAngle++;
	pCAngle++;
//	pGAngle=pGAngle-Segments;
//	pCAngle=pCAngle-Segments;
	pMU=pMU-Segments;
	// advance pointer pBeam;
	pBeam++; }

	//pBeam=pBeam-Beams; // reset pBeam pointer

	cout<< endl;

    delete [] pBeam[j];
	delete [] pBeam;// calls FieldSyn destructor and releases memory (avoid memory "leak")
	return 0;

}
Attached Images
File Type: gif 2D_dynamic_error_array.GIF (9.1 KB, 3 views)
  #2  
Old 19-Feb-2009, 09:25
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Error in execution of 2D dynamic array


You are correct that your delete operations are slightly wrong. You need to delete each FieldSyn object before deleting the array. Something like:
CPP / C++ / C Code:
// Allocate the array of arrays
FieldSyn **pBeam = new FieldSyn*[Beams];

// Allocate each individual array
for (int j=0;j<Beams;j++) 
{
  pBeam[j]=new FieldSyn[Segments];
}





// Now, you have a 2D array of FieldSyn objects
// ....





// Delete each individual array
for (int j=0;j<Beams;j++) 
{
  delete[] pBeam[j];
}

// Delete the array of arrays
delete[] pBeam;
  #3  
Old 19-Feb-2009, 09:41
bobsta bobsta is offline
New Member
 
Join Date: Feb 2009
Posts: 5
bobsta is on a distinguished road

Re: Error in execution of 2D dynamic array


Obvious really!Thanks.
  #4  
Old 19-Feb-2009, 09:56
bobsta bobsta is offline
New Member
 
Join Date: Feb 2009
Posts: 5
bobsta is on a distinguished road

Re: Error in execution of 2D dynamic array


I celebrated too soon! I still have an error after inclusion of the for loop for pBeam[j]. Error message is in German, but it states that the read action to the particular address cannot be performed. Any other ideas? Or is this a quirk of VC++ 6.0?

thanks in advance
Attached Images
File Type: gif 2D_dynamic_error_array.GIF (9.1 KB, 0 views)
  #5  
Old 19-Feb-2009, 10:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Error in execution of 2D dynamic array


Quote:
Originally Posted by bobsta
I celebrated too soon!...

CPP / C++ / C Code:
    FieldSyn **pBeam = new FieldSyn*[Beams];
    for (int j = 0; j < Beams; j++) {
.
.
.
    pBeam[j]->SetGantryAngle(*pGAngle);
.
.
.
    pBeam++;}

You increment pBeam and then you increment j and then you have a statement with pBeam[j]. The memory access is incorrect for the desired functionality, and, furthermore, it goes out of range of the array, causing the memory access violation error manifestation.

1. You should never change the value of a pointer that you obtained from the new operator, since delete must have the same value to work with. If you need to increment it, then make a copy of the pointer and increment the copy. Use the original pointer value in your delete statement.

2. You really don't want to change pBeam anyhow, since you use j to index into the array.

So:

1. Don't increment pBeam.
2. Use the method recommended by fakepoo to delete everything that you allocated.

Regards,

Dave
 
 

Recent GIDBlogNot selected for officer school 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 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
Returning a 2 dimensional Array from a function vicky_brsh C++ Forum 1 04-Jan-2008 14:06
Dynamic Array and Input/Output Files OnlyCurious C++ Forum 11 08-Jan-2007 13:49
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31

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

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


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