:eek: I have revised my program and am getting a run time heap error. Any ideas on what I need to fix?

``````#include <iostream>
#include <assert.h>
#include "rectangle.h"
using namespace std;

CRectangle::CRectangle()
{
size = 0;
pointArray = new CPoint[size];
}

CRectangle::CRectangle(CRectangle& R1)
{
size = R1.getSize();
pointArray = new CPoint[size];

for (int i = 0; i < size; i++)
{
pointArray[i] = R1.getValue(i);
}
}

CRectangle::CRectangle(CPoint P1, CPoint P2)
{
unsigned int width = 0;
unsigned int height = 0;

width = (P1.x - P2.x) + 1;
height = (P1.y - P2.y) + 1;

size = width * height;

pointArray = new CPoint[size];

unsigned int curLocation = 0;
for (int i = P2.x; i <= P1.x; i++)
{
for (int j = P2.y; j <= P1.y; j++)
{
CPoint curPoint (i, j);
pointArray[curLocation] = curPoint;
curLocation++;
}
}
}

CRectangle::~CRectangle()
{
delete pointArray;
pointArray = NULL;
}

unsigned int CRectangle::getSize()
{
return size;
}

CPoint CRectangle::getValue(unsigned int location)
{
return pointArray[location];
}

void CRectangle::test()
{
}

void intersect()
{
//Friend Function to construct intersection of two CRectangles
//The intersection of two CRectangles is the set of points that are
//in both CRectangles.
//intersection R1 R2 = {(x,y)|(x,y) in R1 and (x,y) in R2}
}

void union_function()
{
//Friend Function to determine the union of the two CRectangles.
//The union of two CRectangles is the set of points in both
//CRectangles. union R1 R2 = {(x,y)|(x,y) in R1 or (x,y) in R2)}
}

void diff()
{
//Friend Function to determine the difference of two CRectangles.
//A difference of two retangles is the set of points that are in the
//first CRectangle but not in the second.
//difference R1 R2 = {(x,y)|(x,y) in R1 and (x,y) not in R2)}
}

void concat()
{
//Member Function to concatenate another CRectangle on the
//right.  So R1.concatright(R2) will form the union of all the points
//in R1 and a modification of all the points in R2 The x-coordinate of
//all of the points in R2 are increased by one plus the
//maximum x-coordinate of all of the points in R1.
//R1.concatright(R2) = {(x,y)|(x,y) in R1 or (x',y)
//in R2 and x'= x+1+maxX(R2)}
//where maxX(R2) is the maximum of all the x values of the points
//in R2.  It may notactually be a CRectangle as defined, but we still
//call the result a "CRectangle".
}

void another()
{
//Member Function to concatenate another rectagle above.
//So R1.concatabove(R2) will form the union of all the points
// in R1 and modification of all the points in R2.  The
//y-coordinate of all the points in R2 are increased by one plus the
//maximum y-coordinate of all of the points in R1.  It may not
//actually be a CRectangle as defined, but we
//still call the resule a "CRectangle".
//R1.concatabove(R2) = {(x,y)|(x,y) in R1 or (x, y') in R2 and
//y= y'+1+maxY(R2)} where maxY(R2) is the maximum of
//all of the y values of the points in R2.
}

void CRectangle::printRectangle(CPoint P1, CPoint P2)
{

unsigned int printLocation = 0;
for (int i = P1.x; i <= P1.y; i++)
{
for (int j = P2.x; j <= P2.y; j++)
{
CPoint printPoint (i, j);
pointArray[printLocation] = printPoint;
cout << "x";
printLocation++;
}
}
}

// Point.cpp: implementation of the CPoint class.
//
//////////////////////////////////////////////////////////////////////

#include "Point.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPoint::CPoint()
{
x = 0;
y = 0;
}

CPoint::CPoint(unsigned int x1, unsigned int y1)
{
x = x1;
y = y1;
}

CPoint::~CPoint()
{

}

#ifndef POINT_H
#define POINT_H
#include <iostream>
using namespace std;

class CPoint
{
public:
CPoint();  //constructor
CPoint(unsigned int, unsigned int);
~CPoint(); //destructor
unsigned int x;
unsigned int y;
};
#endif

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
#include "Point.h"
using namespace std;

class CRectangle
{
public:
CRectangle();  //constructor
CRectangle(CRectangle&); //copy constructor
CRectangle(CPoint, CPoint);
~CRectangle(); //destructor
unsigned int getSize();
CPoint getValue(unsigned int);
void test();
friend void intersect();
friend void unionFunction();
friend void diff();
friend void concat();
friend void another();
void printRectangle(CPoint, CPoint);

private:
unsigned int size;
CPoint* pointArray;
};
#endif

#include <iostream>
#include <assert.h>
#include <algorithm>
#include "rectangle.h"
#include "Point.h"
using namespace std;

int main()
{
//Regression Testing
CPoint testP1;

assert(testP1.x == 0);
assert(testP1.y == 0);

CPoint testP2(3,7);

assert(testP2.x == 3);
assert(testP2.y == 7);

CRectangle testR1;

assert(testR1.getSize() == 0);
testR1.test();

//Executing Main Program Function NOW!
CPoint P1(4,3);
CPoint P2(1,1);

CRectangle R1(P1, P2);

return 0;
}``````

Problem:

``delete pointArray;``

You allocate memory with new[], so you should release it with delete[]. Otherwise the behavior is undefined, and because the memory manager is usually very fragile, I wouldn't be surprised if that were your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.