2009-11-22 64 views
0

此問題使用C++語言編寫。 我想動態分配一個指向對象的指針數組。 我知道我可以使用一個vector容器,但演習的一點是不要......將指針數組動態分配到對象

下面是代碼:

void HealthClub::AddHealthClubDevice (char* HealthClubDeviceName) 
{          //We added NumberOfDevices as an attribute, so we won't have to use sizeof all the time 
    if (NumberOfDevices==0) // This is for the first device we want to add 
    { 
     HealthClubDevices = new Device*[1]; 
     HealthClubDevices[0]= new Device(HealthClubDeviceName); 
     NumberOfDevices++; 
    } 
    else  // Here we did realloc manually... 
    { 
     Device** tempHealthClubDevices; 
     tempHealthClubDevices = new Device*[++NumberOfDevices]; //this is where we see the first sign of a problem, The tempHealthClubDevices is not allocated properly 

     for (int i=0 ; i<(NumberOfDevices-1) ; i++) 
     tempHealthClubDevices[i]=HealthClubDevices[i]; 
     delete[] HealthClubDevices;   
     HealthClubDevices = tempHealthClubDevices; 
     HealthClubDevices[NumberOfDevices-1]= new Device(HealthClubDeviceName); 
    } 
} 

設備**對象不正確分配,他們永遠也長不大在規模上,他們總是一個元素。 有誰知道爲什麼? 謝謝!

+0

這個問題每次都會發生嗎?新操作員可能無法分配足夠的內存嗎? – computergeek6 2009-11-22 18:33:18

+0

每當它發生這種情況絕對不是內存問題。 – wazuba 2009-11-22 18:41:49

+0

我沒有看到您發佈的代碼有任何問題。儘管您的評論中提到「將NumberOfDevices添加爲屬性,所以我們不必一直使用sizeof」這令人不安。 sizeof運算符不會給你分配的設備數量。 – 2009-11-22 18:49:51

回答

4

無法重現您的問題。具體而言,這裏的所有的框架代碼我編譯和運行成功 - 你的方法加上最小的腳手架,使之成爲一個完整的程序:

#include <iostream> 

struct Device { 
    char* name; 
    Device(char* n) {name = n;} 
}; 

struct HealthClub { 
    int NumberOfDevices; 
    Device** HealthClubDevices; 
    HealthClub() { NumberOfDevices = 0;} 
    void AddHealthClubDevice(char *); 
}; 

std::ostream& operator<<(std::ostream& o, const HealthClub& h) { 
    o << h.NumberOfDevices << " devices:" << std::endl; 
    for(int i=0; i<h.NumberOfDevices; ++i) { 
    o << " " << h.HealthClubDevices[i]->name << std::endl; 
    } 
    o << "That's all!\n" << std::endl; 
    return o; 
} 

void HealthClub::AddHealthClubDevice (char* HealthClubDeviceName) 
{          //We added NumberOfDevices as an attribute, so we won't have to use sizeof all the time 
    if (NumberOfDevices==0) // This is for the first device we want to add 
    { 
     HealthClubDevices = new Device*[1]; 
     HealthClubDevices[0]= new Device(HealthClubDeviceName); 
     NumberOfDevices++; 
    } 
    else  // Here we did realloc manually... 
    { 
     Device** tempHealthClubDevices; 
     tempHealthClubDevices = new Device*[++NumberOfDevices]; //this is where we see the first sign of a problem, The tempHealthClubDevices is not allocated properly 

     for (int i=0 ; i<(NumberOfDevices-1) ; i++) 
     tempHealthClubDevices[i]=HealthClubDevices[i]; 
     delete[] HealthClubDevices;   
     HealthClubDevices = tempHealthClubDevices; 
     HealthClubDevices[NumberOfDevices-1]= new Device(HealthClubDeviceName); 
    } 
} 

int main() { 
    HealthClub h; 
    std::cout << h; 
    h.AddHealthClubDevice("first"); 
    std::cout << h; 
    h.AddHealthClubDevice("second"); 
    std::cout << h; 
    h.AddHealthClubDevice("third"); 
    std::cout << h; 
    return 0; 
} 

編譯罰款,即使--pedantic,並在運行時發出:

$ ./a.out 
0 devices: 
That's all! 

1 devices: 
    first 
That's all! 

2 devices: 
    first 
    second 
That's all! 

3 devices: 
    first 
    second 
    third 
That's all! 

根據需要。所以,你的問題的原因必須在別處。考慮到你的真實程序失敗(你沒有告訴我們如何)以及這個最小成功的程序,你可以「通過二分法插入」來構建最小的失敗案例 - 如果仍然不能告訴你問題出在哪裏,發佈最小失敗案例和比它小的一個epsilon,它仍然成功地作爲SO問題,可以肯定地爲您提供所需的幫助(確保也指定編譯器,操作系統等)。

+0

非常感謝! 我的問題是顯示數組...哇! – wazuba 2009-11-22 19:05:23