bksy bksy - 11 days ago 4x
C++ Question

C++ - Resizing a dynamic array


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;

if (pElements)
void AddGraphicElement();
void DeleteGraphicElement();
void ReportVectorGraphic();

The following class definition for a GraphicElement:

static const int SIZE = 256;
unsigned int numLines;
Line* pLines;
char name[SIZE];
numLines = 0;
pLines = nullptr;
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

//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;

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?



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;


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.