0

Just as it's possible to create a new instance of an object in the heap using new Class(), is it possible to create an object in the stack?

I'm learning about heap vs stack.

One way, in C++, is to have a static member in the cpp code:

static MyObject myObject;

but suppose that I have this:

static int emptyArray[100000];

there's sufficient RAM space, not allocated by the OS, for me to build my object. Can I use a custom allocator and build my object in a subset of this array? How would I do that?

Paprika
  • 402
  • 5
  • 18
  • Yes. https://github.com/Ambeco/mpd/blob/master/SmallContainers/erasable.hpp – Mooing Duck Mar 29 '21 at 17:34
  • 4
    Have a look at the [placement new](https://en.cppreference.com/w/cpp/language/new) section. – super Mar 29 '21 at 17:34
  • Does this answer your question? [C++ How to allocate memory dynamically on stack?](https://stackoverflow.com/questions/6335023/c-how-to-allocate-memory-dynamically-on-stack) – Omid Mar 29 '21 at 17:35
  • 4
    The object created by`static MyObject myObject;` is not on the stack. It's also not on the heap. There's another region of storage for objects with static storage duration. – Pete Becker Mar 29 '21 at 17:49
  • As @super says, but be sure to note the use of `alignas`. – jxh Mar 29 '21 at 17:50
  • @MooingDuck can you explain to me what this code does? – Paprika Mar 29 '21 at 18:13
  • 1
    tldr; `mpd::erasable myObject = Square(10);`. It allows you to store values with derived types on the stack, using a fixed size buffer. – Mooing Duck Mar 29 '21 at 18:16

1 Answers1

2

Just as it's possible to create a new instance of an object in the heap using new Class(), is it possible to create an object in the stack?

Sure, simply declare it, without static, as a local variable inside of a function, eg:

void doSomething()
{
    ...
    MyObject myObject; // <-- allocated on the stack
    ...
}

suppose that I have this:

static int emptyArray[100000];

there's sufficient RAM space, not allocated by the OS, for me to build my object. Can I use a custom allocator and build my object in a subset of this array?

You don't need a custom allocator, you can use placement-new for that, eg:

static int emptyArray[100000];
...
MyObject *myObject = new (&emptyArray[offset]) MyObject; // <-- allocated inside of emptyArray
...
myObject->~MyObject(); // <-- must call destructor manually

Just make sure that offset is an even multiple of MyObject's size and alignment. You can use std::aligned_storage to help you with that, eg:

#include <type_traits>

static const int MaxObjects = ...;
static std::aligned_storage<sizeof(MyObject), alignof(MyObject)>::type emptyArray[MaxObjects];
...
MyObject *myObject = new (&emptyArray[index]) MyObject;
...
myObject->~MyObject();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • does this mean that I can use C++ to dynamically create objects in microcontrollers using `new` even though they have no OS to handle memory allocation? I can just pass an array of enough size? – Paprika Mar 30 '21 at 05:11
  • @Paprika yes. When using `new`/`delete`, the compiler allocates/frees memory for objects. When using `placement-new`, you handle that yourself instead. – Remy Lebeau Mar 30 '21 at 14:16