1

I'm new to optimization in C++. I have read that stack allocated memory can be much faster than heap allocated memory.

I also have read that std::array is stack allocated, but most other containers, like std::vector or dynamic arrays are heap allocated. I'd like to define a class which essentially just stores an array of doubles. I intend for all instances of the class to have the same dimension, and that I can calculate what that dimension will be at compile time. The catch is that I would like to do the computation of that dimension in main.cpp instead of in the class.cpp. That means trying something like the following:

class.h:
extern constexpr dimension;

and,

class.cpp:
class Coordinates {
    public std::array<double, dimension> q{}; 
}

and then

main.cpp:
#include "class.h"
constexpr dimension = 2*3*100

Now, extern constexpr dimension is nonsense as the translation unit will not know what the value of dimension is at compile time.

Is there a way to have a stack-allocated array type object with dimension defined in another translation unit? Would it even be worth it?

10
  • How about vector with stack allocator? Using alloca()? Mar 26, 2021 at 18:47
  • Best thing I can think of is main allocates the array, since only it knows the size, and provides it to the object on construction. I think you're stuck on this one. Mar 26, 2021 at 18:48
  • I strongly doubt std::array is stack allocated because you would have to somehow pass the result of alloca() to the std::array<> object. Where did you hear such information? That being said you can stack allocate almost anything with the correct in-place constructor and alloca(). STL doesn't support stack allocation because it is a very dangerous practice.
    – David Bien
    Mar 26, 2021 at 18:50
  • 1
    std::array can be allocated anywhere. Could be stack, could be static storage, could be dynamic storage (new std::array<>)
    – SergeyA
    Mar 26, 2021 at 18:53
  • 1
    "I intend for all instances of the class to have the same dimension" then use template<int dimension> class Coordinates{} Mar 26, 2021 at 18:54

1 Answer 1

0

You are asking for a template:

template<int dimension>
struct Coordinates {
    std::array<double, dimension> q{}; 
};

Usage:

int main()
{
    constexpr auto dimension = 2*3*100;
    Coordinates<dimension> c;
    c.q[0] = 1;
    c.q[1] = 2;
    // ...
}

Demo

1
  • This does exactly what I was asking for! I didn't realize that templates can be non-type variables. This advantage (static allocation instead of dynamic) is pointed out in this article on the topic: learncpp reference
    – denomme
    Mar 27, 2021 at 3:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.