0

I want to have a stack-allocated array initialized by a copy constructor.

I only see methods allocating memory on the heap, or using std::array.

With std::array, it would look like the following:

class A
{
    std::array<int, 5> my_array;    // I would like to have int my_array[5]; instead of the std::array
    int size;
public:
    A(const A& p)
        : my_array{ p.my_array }, size(p.size) {}
}

How can I implement this without std::array<int,5> but with a plain array (int my_array[5];)? I have added this in the comment in the code.

At the moment, the array contains integers. If this would contain, let's say a class B, which contains also a pointer:

class B
{
   int* my_ptr;
}

Does std::array handle this correctly and perform a deep copy?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Chris
  • 1
  • 2
  • 4
    What's wrong with using `std::array`?? What are you missing in preference of raw arrays? If you need dynamic allocation at runtime, rather use `std::vector`. – πάντα ῥεῖ Feb 07 '22 at 23:56
  • Why would you want to. Std::array is proper modern c++. – Taekahn Feb 08 '22 at 00:07
  • 1
    In your second question, it's `class B` itself that is wrong, not `std::array`. Your `B` class needs to define its own copy/move semantics in order to be used safely. – alter_igel Feb 08 '22 at 00:07
  • One Stack Overflow Question should contain one question. – user4581301 Feb 08 '22 at 00:09
  • Whether an array is "stack allocated" or not (whether that is an `std::array` or a raw array) depends on how the instance of your class `A` is instantiated. If the instance of `A` is dynamically allocated, the array and its elements are likely to also occupy dynamically allocated memory. Arrays elements are copied by value - if you need a deep copy, then the elements (or more precisely the copy constructor, etc, of their type) need to do a deep copy. Your class `B` contains a pointer so its constructor needs to implement a deep copy if you need that. – Peter Feb 08 '22 at 00:11
  • Copying `B` will require a copy constructor to copy whatever `my_ptr` points at *if this is the required behaviour*. In order to discuss whether or not `B` needs special copying logic, we need to know who [owns](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) the object `my_ptr` points at.If `B` owns the object, the full set of special member functions([Rule of Three or Five](https://en.cppreference.com/w/cpp/language/rule_of_three)) is required. If not go with the Rule of Zero. The owner will manage the object. – user4581301 Feb 08 '22 at 00:18
  • `std::array ` is just a safety wrapper around `int[5]` – pm100 Feb 08 '22 at 00:57

1 Answers1

1

Arrays cannot be copy-initialised in C++. You can either:

  • Assign each member in a loop i.e. std::copy in the constructor body.
  • Or wrap the array inside a class, and use the generated copy constructor. There is a template for such wrapper class in the standard library. It's the std::array that you already know of.

Of course, your class itself is a class that is a wrapper for the array, so you could simply not have user defined copy constructor, and instead use the implicitly generated one:

struct A
{
    int my_array[5];
    int size;
};

If this would contain, let's say a class B which contains also a pointer ... does the std::array handle this correctly

Yes.

... and performs a deep copy?

No. Copying a std::array copies each element and nothing more. Copying a pointer is a shallow copy.

eerorika
  • 232,697
  • 12
  • 197
  • 326