C++ - 调整动态数组的大小
C++ - Resizing a dynamic array
已解决
我在调整指针数组的大小时遇到问题,主要是 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
中的对象都指向同一位置。然后删除该位置,从而显示您看到的未定义行为。您需要进行深度复制。
相关文章:
- std::向量与传递值的动态数组
- 将数组动态分配到具有指针参数的函数中
- 我可以以某种方式使我的矢量/数组动态更改装置数量吗?
- 使用 new: "potentially uninitialized pointer"将对象数组动态分配给指针
- 为什么C 没有方便的方式来为多维数组动态分配内存
- 如何制作具有动态大小的数组?动态数组的一般用法(可能还有指针)
- c++中的数组动态内存分配
- 多维数组动态分配的内存图
- 如何在 c++ 中使用 2-D 固定数组为 4-D 数组动态分配内存
- C++ 我在数组/动态内存方面遇到了一些问题
- C++ 中从数组动态强制转换
- C++中高维数组动态内存分配的正确方法.
- 从 C++ 结构中包含的指针数组动态分配和访问内存
- 对象数组动态内存分配
- 为二维数组动态分配内存时出错
- 如何将2d数组动态分配给结构
- 为结构数组c++动态分配内存
- 将数组动态添加到多维数组
- 类中的类数组-动态数组(c++)有问题
- 分配对象数组动态冻结