2009-11-22 65 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