High-level code is relative. There's no reason not to abstract away memory management in its own class. The high-level class's memory layout and the performance will be identical to what you'd get if you called new directly in its constructor.
However, by abstracting it away, you eliminate an entire class of bugs. Which is important for this exact reason:
If you follow the rule of zero, you cannot miss it.
I know about the need to create (or disable) copy constructors and the assignment operator, but to be honest, this is the first time I've heard about the rule of zero. For example, I don't see where in the TestClass there are default operators that I should avoid.
Looks like I need to look into this theme more seriously.
On an AVR without access to the STL, that's really the only practical option.
You also need to check against failure of the allocation. On an AVR, a simple check against nullptr appears to be sufficient:
ptr = reinterpret_cast<uint8_t *>(new uint8_t[5000]);
if (ptr == nullptr) {
Serial.println("Allocation Failed");
while (1) {
}
}
However, I've found that to not always be the case with ARM / ESP. I'm told that's because in Big Computer C++, the new operator throws an exception on allocation failure rather than returning nullptr. Most Arduinos (including ARM / ESP) don't have exceptions enabled by default (if it's even possible at all). So, 'new' just returns. Problem is, the compiler / optimizer is smart enough to know that (in Big Computer C++), 'new' never returns nullptr. So, the null check is simply optimized away. Thus, I've taken to using:
Arduino is a great beginners tool to get projects started. What's a good way to start digging in to these more complicated concepts? There is a lot of information out there on objects and classes in C++. Where's a good place to get started? Should I be looking at developing my basic code skills in C++ before applying these more complicated concepts to Arduino? Arduino (and this forum) made things very easy to get started with the basics. Is there something similar for C++? Especially something like this forum. There seems to be an overwhelming amount of c++ forums, which is best?
Constants are only visible inside of the current translation unit. Having the same constant defined in multiple translation units do not cause multiple definition errors.
This does not do what you think it does. It accesses the (nonexistent) element beyond the end of the array, which is not allowed.
Saving the size is not useful, because it is constant.
This simply defines and initializes a local variable that's never used, which is probably not what you intended. It's not clear what you want it to do, though. If you want to change the size of the array from the sketch, why use a constant instead of the template approach suggested by gfvalvo?