Am I right that
Object* obj = new Object();
is created on the heap
Yes-ish. It is created in a dynamic memory store which may or may not be a heap. I don't know Arduino, but it's probably a heap. This is mostly a terminology issue because the C++ standard ensures the observable behaviour will be identical regardless of what storage methodology is used. The same applies to stack. C++ doesn't care what you use for automatic storage so long as it has the specified behaviour.
bar is still the exact same pointer to "obj" (which i created above) on the heap?
Yes, though the compiler may play all sorts of fun and games behind the scenes if it is impossible for anyone to tell the difference. This is called the "as-if" rule. What exactly is the "as-if" rule?
If this is right, i also have to call
delete obj;
for memory clearance right?
Terminology niggle (clearance implies overwriting the memory with some sort of blank pattern. You're freeing it) there, but yes. Someone has to give the memory back be it you or the runtime on exit. Typically it's best if you do it to prevent leaks of memory and other resources.
You may find released memory is the least of your worries. What if the object represents some external shared resource that will be held even after the process terminates? Sucks tio have to reboot the server because something didn't get unlocked. One of the staples of C++ is RAII (What is meant by Resource Acquisition is Initialization (RAII)?), an ideological solution to this problem.
You may also automate the process with Smart Pointers. What is a smart pointer and when should I use one?
Another important point: Unlike many other languages, you do not have to new
objects. The vast majority of times a simple Object obj;
will be the correct choice. obj
will be created in temporary storage and the runtime will take care of destroying obj
when it goes out of scope. Scope it correctly and you'll never have a problem. You may have a problem deciding what exactly is the best scope, though.