0

I would like to have a class that contains an array member, but the constructor lets me set the size of an array member.

Is this doable? I do not thing I need dynamic allocation, since once the class instances are created, there is no need for the array to change size, it is just that each class instance will have a different size.

8
  • 1
    A class has one size. Period. If the size is determined at runtime, even if it is fixed after the determination, that's dynamic. Whatever you do to change the size without a dynamic allocation either isn't doing what you think it's doing or is abusing some manner of undefined behaviour. Commented Apr 23, 2021 at 21:28
  • since once the class instances are created -- I guess you missed this first step. How will the array be created if there isn't some sort of "dynamic" size determination? Just the creation of the array is dynamic in itself. What you do after the array is created is your business, whether you want to resize them or not. Commented Apr 23, 2021 at 21:31
  • 1
    So not doable. What's your actual goal? Maybe we can suggest alternatives or work-arounds. Commented Apr 23, 2021 at 21:33
  • @user4581301 I wanted a class with an array of lambdas, managing on-screen widgets. but these widgets could have a various amount of buttons that trigger different effects, so some could need 1 lambda, while others 3 or 4! but this is for an embedded system, so using a vector of lambda didn't work because vectors are not available in this environment.. Commented Apr 23, 2021 at 21:46
  • 1
    You can statically allocate an object that contains an array of whatevers that is the largest reasonable size. If you know you could have up to three or four whatevers, you allocate an array of 4. If you don't know the actual peak, you make a guess or give up on static allocation and make your own simple vector with correct copy and move semantics that observes RAII. You don't need it to be resizable or iterators and the rest of the vector goodies, so it's a relatively simple job. Commented Apr 23, 2021 at 22:59

1 Answer 1

1

Despite several comments suggest that this would be impossible, it is actually not impossible.

The simplest way, of course, is to use an indirection and allocate the array during construction just the normal way (with a = new type[size] and calling delete[] a - not delete a - in the destructor).

But if for some reason you really do not want to have the array data being allocated separately from your object, you can use placement-new to construct your object into a pre-allocated buffer that is large enough to contain all your elements. This avoids a separate allocation for your array and you can still have dynamic size.

I would not recommend using this technique, though, unless you really have a demanding use case for it.

Sign up to request clarification or add additional context in comments.

3 Comments

If I'm reading you right you are suggesting what's called in C a flexible array member. This violates the C++ language rules. Can you do it? Yes, but the language gives no guarantees about the results. You cannot make an array of these objects because arrays count on an object being fixed size (and correct alignment, etc...). Of course you can work around that as well, but you won't have a simple array of objects anymore.
One additional small note: When Paul and I refer to something as impossible above in the comments, we're referring to statically allocating a structure of dynamic size. There are tricks like over-provisioning you can use, but they still can only hold so much (and usually wind up wasting a lot of space). As soon as you work around this limitation by pulling in new, malloc or whatever, static allocation went right out the window.
@user4581301 yes, very true. That is why I don't recommend it. You can have C-style flexible arrays by enabling the respective extension in your compiler. Another, less favorable, trick would be to declare the array with size 1 and apologize to your static code analyzers for what seems to be of bounds reads. Of course, in 99.999% of all imaginable cases, just separately allocating the array should be preferred.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.