0

SOLVED

I'm having problems with resizing an array of pointers, mainly with the last few lines of the AddGraphicElement() function.

I have the following class definition for a VectorGraphic:

unsigned int numGraphicElements;
GraphicElement* pElements;
public:

VectorGraphic();
~VectorGraphic()
{
    if (pElements)
        delete[]pElements;
}
void AddGraphicElement();
void DeleteGraphicElement();
void ReportVectorGraphic();

The following class definition for a GraphicElement:

static const int SIZE = 256;
public:
unsigned int numLines;
Line* pLines;
char name[SIZE];
GraphicElement()
{
    numLines = 0;
    pLines = nullptr;
}
~GraphicElement()
{
    if (pLines)
        delete []pLines;
}

And the following function AddGraphicElement() in the VectorGraphic class:

unsigned int numLines;
char name[256];
cout << "Adding a Graphic Element" << endl;

//Name input
cout << "Please enter the name of the new GraphicElement(<256 characters): ";
//Flush cin so it doesnt skip getline
cin.ignore(); 
cin.getline(name,sizeof(name));

//Line input
cout << "How many lines are there in the  new GraphicElement? ";
cin >> numLines;

//Allocate memory for line(s)
Line* pLines = new Line[numLines];

for(int i=0;i<numLines;i++)
{
    //Start point input
    cout << "Please enter the x coord of the start point of line index " << i << ": ";
    cin >> pLines[i].start.x;
    cout << "Please enter the y coord of the start point of line index " << i << ": ";
    cin >> pLines[i].start.y;
    //End point input
    cout << "Please enter the x coord of the end point of line index " << i << ": ";
    cin >> pLines[i].end.x;
    cout << "Please enter the y coord of the end point of line index " << i << ": ";
    cin >> pLines[i].end.y;
}

//Allocate new size for GraphicElement*
GraphicElement* newElements = new GraphicElement[numGraphicElements+1];

//Copy old elements to new pointer
for(int i=0;i<numGraphicElements;i++)
{
    newElements[i] = pElements[i];
    newElements[i].pLines = pElements[i].pLines;
}

//Assign new element to last index
strcpy(newElements[numGraphicElements].name, name);
newElements[numGraphicElements].numLines = numLines;
newElements[numGraphicElements].pLines = pLines;

//Re-assign the pointer and increment number of elements    
delete[] pElements;
pElements = newElements;    
numGraphicElements++;

Everything up until the last 3 lines seems to work fine. If I print the contents of newElements (before the delete and reassignment), all of my data is there. However, if I print it after delete and reassignment, my data is lost (the junk value -17891602 is there instead).

I don't think I'm using delete[] correctly since removing this line lets my program work, albeit with memory leaks:

delete[] pElements;

I suppose what I'm asking is how do I properly use delete[] in my program?

Thanks!

EDIT: SOLVED, NEW CODE FOR LOOP BELOW

    strcpy(newElements[i].name, pElements[i].name);
    newElements[i].numLines = pElements[i].numLines;

    Line* newLines = new Line[newElements[i].numLines];
    newLines->start.x = pElements[i].pLines->start.x;
    newLines->start.y = pElements[i].pLines->start.y;
    newLines->end.x = pElements[i].pLines->end.x;
    newLines->end.y = pElements[i].pLines->end.y;

    newElements[i].pLines = newLines;
bksy
  • 33
  • 3
  • 2
    What you want to do is separation of concerns. Your class shouldn't manage dynamic arrays, it should use something that does the management for you. That something is `std::vector`. – GManNickG Oct 15 '16 at 03:35
  • 1
    What is [The Rule Of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)? Read the link and find out – user4581301 Oct 15 '16 at 06:29

1 Answers1

1

When you delete pElements it is deleting each GraphicElement object in there. When you delete a GraphicElement it deletes its pLines member. That's fine, except when you copy the data from pElements into newElements you only copy the pLines pointer value. So all of the old GraphicElement objects in pElements and the ones in newElements point to the same location. You then delete that location giving you the undefined behavior you're seeing. You need to do a deep copy.

Michael Albers
  • 3,721
  • 3
  • 21
  • 32