【问题标题】:How do I initialize a dynamically allocated array within a class constructor如何在类构造函数中初始化动态分配的数组
【发布时间】:2017-01-18 21:46:42
【问题描述】:

我们应该为每个索引分配一个空字符串,然后替换 在函数 addB() 中有一个值。 我对此很陌生,所以我遇到了很多麻烦。

class A //in a.h

{

  private:

    B * b;

    int maxNumberOfItems;

    //...

  public:

  A();

  ~A();

 void addB(const B & something);

};

//in a.cpp

 A::A()

  {

    maxNumberOfItems=10;
    for(int i=0;i<maxNumberOfItems;i++)
    {
       b[i]="";//has to be an empty string, I am getting a segmentation fault
    }

  }

  A::~A(){/*...*/}

  //...

//in b.h

class B
{

 private:

      string name;

      int price;

  public:

      void setName(string);

      string getName();

      void setPrice();

      int getPrice(int);

      B & operator=(string &);

};

//in b.cpp

B & B::operator=(string & a){name = a;price = 0; return *this;}
//...

这只是显示我的问题的程序的 sn-p

【问题讨论】:

  • b 似乎是一个未初始化的指针...您想知道如何分配内存吗?
  • 您的b 未分配。阅读operator new[]
  • 房间里的大象:如果可能,使用std::vector 代替动态数组。

标签: c++ segmentation-fault initialization dynamic-memory-allocation


【解决方案1】:

你应该在使用动态数组之前分配内存。我已经为 b 分配了内存

class A //in a.h

{

private:

    B * b;

    int maxNumberOfItems;

    //...

public:

A();

~A();

void addB(const B & something);

};

//in a.cpp

A::A()

{
    maxNumberOfItems=10;
    b = new B[maxNumberOfItems];

    for(int i=0;i<maxNumberOfItems;i++)
    {
    b[i]="";//has to be an empty string, I am getting a segmentation fault
    }

}

A::~A(){/*...*/}

//...

//in b.h

class B
{

private:

    string name;

    int price;

public:

    void setName(string);

    string getName();

    void setPrice();

    int getPrice(int);

    B & operator=(string &);

};

//in b.cpp

B & B::operator=(string & a){name = a;price = 0; return *this;}

【讨论】:

  • 不需要循环。 b = new B[maxNumberOfItems]();
【解决方案2】:

看起来class A 应该是一个动态数组类。

当您创建 A 的新实例时,您还必须为数组 b 分配内存。这只是指向内存中某个点的指针。从您发布的代码来看,它没有被初始化,并且可以指向任何随机内存位置 - 这不好(即您的段错误的可能原因)。

我建议进行以下更改。

A::A(){
  maxNumberOfItems=10;
  b = new B[maxNumberOfItems]; // b is an array of B objects.
                               // with default constructed values
  // This way avoids the need for using a for loop and reassigning values
}

~A(){
  if (b != NULL) { delete b; }
}

class B{
  private:
    //....
  public:
     B(): name(""), price(0) {}
    // Although the default constructor without defining one should behave the same.
    // This just makes it explicit. what name and price default to.
}

【讨论】:

    【解决方案3】:
    maxNumberOfItems=10;
    //add this line to your code
    b = new B[maxNumberOfItems];
    //do some error check stuff
    for(int i=0;i<maxNumberOfItems;i++)
    {
       b[i]="";//has to be an empty string, I am getting a segmentation fault
    }
    

    你没有为 b[i] 分配内存,所以你得到一个段错误。

    【讨论】:

    • 谢谢,我完全忘记分配内存了。
    • @ArcheAngel 完成后不要忘记删除分配的内存。
    • 不需要循环。 b = new B[maxNumberOfItems]();
    • @Timothy Murphy,谢谢你,我没有在我的问题中添加它。我用 //... 来引用不相关的代码。
    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多