3 ms·
The default arduino toolchain, which is a g++ wrapper, supports C++, even though the whole system only has only 1 kilobyte of RAM. The C++ 'new' operator does
by londons_explore 4y ago
The default arduino toolchain, which is a g++ wrapper, supports C++, even though the whole system only has only 1 kilobyte of RAM.
The C++ 'new' operator does seem to be supported, so I assume it has some kind of heap allocator - although I imagine that when you've only got 1 kilobyte to play with it gets quickly eaten by the allocators datastructures and C++ vtable pointers, not to mention the impact of fragmentation if you allocate any objects more than a few tens of bytes.
- Sukera 4y agoI'm not as familiar with how C++ does allocations compared to julia, but I'd imagine the limitations would be similar - if `new` can allocate memory on the stack, I can't imagine why it wouldn't work out of the box. C++ really isn't my forte though.
- pjmlp 4y agoWith placement new you can allocate wherever you want, as the memory region is one of the parameters. You can also overload it, so that it can be given as implicit parameter. Modern C++ compilers are also able to do escape analysis and remove new altogether, https://godbolt.org/z/f9vnxG88K https://godbolt.org/z/f9vnxG88K If you try AVR instead of X64, the optimization will be missing, which is only a side effect of the backend not having as much attention in what it looks into.
- londons_explore 4y agoIf you comment out the delete[] line, it returns the same output. Does that mean leaking memory is undefined behaviour?
- pjmlp 4y agoLeaking memory is always undefined behaviour regardless of the language when talking about manual memory management. What guarantees can you assure about the state of the application? In any case, in this specific example for x64 it doesn't matter, because new gets optimized away. However, if that optimization isn't done, e.g. AVR backend, then nasal daemons will be summoned.
- pjmlp 4y agoWith placement new you don't need an heap, and using C++ doesn't require to use classes, there is still so much improvements over bare bones C. In any case, when all we have is 1 KB, the real answer is Assembly.
- skybrian 4y agoIt's kind of a silly exercise anyway nowadays when, for example, a Raspberry Pi Pico is $4 and has 256K RAM. And there many other nice 32 bit boards.