c++ - 对象指针数组的动态分配

标签 c++ arrays dynamic pointers

这道题是用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);
   }
}

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

最佳答案

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

#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 问题仍然成功肯定可以为您提供所需的帮助(一定要指定编译器、操作系统等)。

关于c++ - 对象指针数组的动态分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1779494/

相关文章:

javascript - 设置动态创建的 div 元素的样式

c# - XML-RPC.NET 和 C# 动态类型

c++ - 多维数组麻烦(bounds)

c++ - 传递对象成员

c++ - 尝试使用成员函数访问动态分配的成员变量时发生读取访问冲突

ios - 如何检查两个数组中是否存在公共(public)值 - 在 SWIFT 中?

javascript - 使用javascript创建动态多维关联数组

c++ - 在函数声明中使用 "this"(作为默认参数)

c++ - mingw-w64 和 SDL2 链接问题

javascript - 使用 Jquery 或 Javascript 保存数据