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;
std::vector.