【问题标题】:Dynamic Allocation of an Array of Pointers to Objects指向对象的指针数组的动态分配
【发布时间】:2009-11-22 18:21:26
【问题描述】:

这个问题是用 C++ 编写的。 我正在尝试动态分配指向对象的指针数组。 我知道我可以使用矢量容器,但练习的重点不是......

代码如下:

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

Device** 对象没有正确分配,它们的大小永远不会增长,它们始终是一个元素。 有谁知道为什么? 谢谢!

【问题讨论】:

  • 是不是每次都会出现这个问题?有没有可能是 new 运算符无法分配足够的内存?
  • 每次都会出现这种情况,绝对不是内存问题。
  • 我没有发现您发布的代码有任何问题。尽管您的评论说“添加 NumberOfDevices 作为属性,所以我们不必一直使用 sizeof”令人不安。 sizeof 运算符不会给您分配的设备数量。

标签: c++ arrays dynamic pointers


【解决方案1】:

无法重现您的问题。具体来说,这里是我编译并成功运行的所有骨架代码——你的方法加上最小的脚手架使它成为一个完整的程序:

#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 问题肯定可以为您提供所需的帮助(请务必同时指定编译器、操作系统等)。

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 2012-03-09
    • 2013-05-26
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    相关资源
    最近更新 更多