C++ - 调整动态数组的大小

C++ - Resizing a dynamic array

本文关键字:数组 动态 调整 C++      更新时间:2023-10-16

已解决

我在调整指针数组的大小时遇到问题,主要是 AddGraphicElement(( 函数的最后几行。

我有以下矢量图形的类定义:

unsigned int numGraphicElements;
GraphicElement* pElements;
public:
VectorGraphic();
~VectorGraphic()
{
    if (pElements)
        delete[]pElements;
}
void AddGraphicElement();
void DeleteGraphicElement();
void ReportVectorGraphic();

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

以及 VectorGraphic 类中的以下函数 AddGraphicElement((:

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

直到最后 3 行之前的所有内容似乎都工作正常。如果我打印 newElements 的内容(在删除和重新分配之前(,我的所有数据都在那里。但是,如果我在删除和重新分配后打印它,我的数据就会丢失(垃圾值 -17891602 在那里(。

我认为我没有正确使用 delete[],因为删除这一行让我的程序可以工作,尽管有内存泄漏:

delete[] pElements;

我想我要问的是如何在我的程序中正确使用 delete[]?

谢谢!

编辑:已解决,下面的循环新代码

    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;

当您删除pElements时,它会删除其中的每个GraphicElement对象。删除GraphicElement时,它会删除其pLines成员。这很好,除非将数据从pElements复制到newElements只复制pLines指针值。因此,pElements中的所有旧GraphicElement对象和newElements中的对象都指向同一位置。然后删除该位置,从而显示您看到的未定义行为。您需要进行深度复制。