2016-10-15 86 views
0

解決C++ - 調整大小動態數組

我在與調整指針的數組,這主要與AddGraphicElement()函數的最後幾行的問題。

我有一個VectorGraphic以下類定義:

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

而下面的函數AddGraphicElement()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++; 

一直到最後3行似乎工作正常。如果我打印newElements的內容(刪除和重新分配之前),我的所有數據都在那裏。但是,如果我在刪除和重新分配後打印它,我的數據就會丟失(取而代之的是垃圾值-17891602)。

我不認爲我使用delete []正確,因爲刪除此行讓我的工作方案,儘管有內存泄漏:

delete[] pElements; 

我想我要問的是如何正確地做我在我的程序中使用delete []?

謝謝!

編輯:解決了,新代碼,實現LOOP低於

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

你想要做的是分離擔憂。你的班級不應該管理動態數組,它應該使用一些爲你管理的東西。這是'std :: vector'。 – GManNickG

+1

什麼是[三條規則](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-reeree)?閱讀鏈接並找出 – user4581301

回答

1

當你刪除pElements它刪除每個GraphicElement對象在那裏。當您刪除GraphicElement時,它將刪除其pLines成員。這很好,除非將數據從pElements複製到newElements,您只能複製pLines指針值。因此pElements中的所有舊GraphicElement對象指向newElements中的對象指向相同的位置。然後,刪除該位置,使您看到未定義的行爲。你需要做一個深層複製。

+0

謝謝,我明白了! – bksy