Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array in Constructor

Tags:

c++

oop

What happens in the following code?

I guess it doesn't work since I get a segmentation fault if I want to add something to the b array, but what exactly did I do here?

Is there no way of specifying the size of an array inside the constructor?

class A {
  public:
   A() {
      b[3];
   }
  private:
    B b[];
};
like image 999
user695652 Avatar asked Aug 01 '26 10:08

user695652


2 Answers

B b[] here is a "flexible array member", a non-standard extension in your compiler (taken from C99) that allows you to declare an unbounded array as the last member in a type. It's only of use when allocating your object the old-fashioned C way (when you pad your argument to malloc to make space for the array), and ought to be avoided. In this case, you haven't allocated any extra memory for the array, so in your constructor body when you're trying to access something that occurs 3 'elements' past this nothingness, you're invoking UB. I'll ignore the extension for the rest of my answer, as it really has no place in C++ code.

Is there no way of specifying the size of an array inside the constructor?

No, there isn't.

Array bounds must be known at compile-time, so there is no case where you know more in your ctor body than you do in the class definition; you are required to write the dimension in the member's declaration itself:

class A {
    B b[3];
};

If the dimension is a run-time quantity in your program, you'll need to instead store a pointer and, in your constructor, point it at a dynamic block of memory:

class A {
  public:
   A() : b(new B[3]) {}
  ~A() { delete[] b; }
  private:
    B* b;   // same as `B b[]`! but far clearer
};

Instead, though, I suggest a std::vector:

class A {
  public:
   A() : b(3) {}
  private:
    std::vector<B> b;
};
like image 150
Lightness Races in Orbit Avatar answered Aug 04 '26 01:08

Lightness Races in Orbit


Yes. Using operator new: b = new B[3];. Declare b as B *b for that. Of course, you need to delete[] it in the destructor.

But a better way would be using std::vector instead of the array, and then you don't need to worry about preallocating memory.

like image 22
littleadv Avatar answered Aug 03 '26 23:08

littleadv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!