3

I'm new to C++, and coming from Delphi.

Am I right that

Object* obj = new Object();

is created on the heap and that

void foo(Object* bar)
{
    // bar is still the same pointer to "obj" (which I created above) on the heap?
}

foo(obj);

If this is right, I also have to call

delete obj;

for memory clearance, right?

Douglas
  • 1,134
  • 9
  • 25
MaxMadman
  • 224
  • 3
  • 10
  • 4
    That seems correct. – Jiminion Nov 18 '16 at 18:58
  • Yes, everything seems in order (except that "memory clearance" isn't really the term we use). Everything you `new` you need to `delete` to avoid memory leaks and passing an object via pointer to a function means the function accesses the same object. – UnholySheep Nov 18 '16 at 19:01
  • 1
    As I understand it, C++ uses "automatic" and "dynamic" memory, and does not use terms heap/stack. – 2785528 Nov 18 '16 at 19:04
  • Is there a question? See [MCVE] and other guidance about SO. – 2785528 Nov 18 '16 at 19:05
  • @DOUGLASO.MOEN Hold your horses Douglas (that's a joke). I myself come from a Delphi background and cpp to me was a revelation. The above question is a valid one IMHO. I am also interested to know what do you think about how the memory is organized on Arduino board with an Atmel microcontroller. Perhaps you can shed some light on the subject? –  Nov 18 '16 at 19:18
  • @RawN -- And yet, how can we help the OP? it seems the answer is either 'yes' (or no). See the help section, there is lots of guidance about asking a question. For example, "To prevent your question from being flagged and possibly removed, avoid asking subjective questions where ;; your answer is provided along with the question, and you expect more answers: ;; there is no actual problem to be solved: ;; you are asking an open-ended, hypothetical question: ;; your question is just a rant in disguise: ;;" [ I miss working with Delphi ] – 2785528 Nov 18 '16 at 19:23
  • @DOUGLASO.MOEN I agree with you. Yet there is a big mountain to climb when switching from Delphi to cpp. Although available there is barely any use of pointers in Delphi for the most part so in that way I sympathize with OP and can understand why he asked this. –  Nov 18 '16 at 19:31
  • @RawN - C++ is target agnostic, so perhaps this question belongs elsewhere. I have worked in embedded systems, from small to big, and each with unique memory issues. In embedded systems, memory organization is set-up by the BSP (board support system, with options to modify). vxWorks is my favorite tool suite. Arduino is fun and useful because it is simple. – 2785528 Nov 18 '16 at 19:34
  • Thanks @Raw N, I'm happy that I'm not the only one who gets confused at this point when coming from Delphi! :D I understand that the question might sound odd or not complete, but in Delphi this works quiet diffrently and when I learn a new language/envoirement, I'd like to learn it right. Then sometimes such odd questions occur. Thanks to everyone! :) – MaxMadman Nov 19 '16 at 03:36

1 Answers1

4

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.

Community
  • 1
  • 1
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • Thanks a lot for your detailed answer. In fact i knew about smart pointers, but as I was "scared" of c++ (which isnt actually that scary. lol) my first toughts were to better dont use it because it will be too complicated. But now I think I got it mostly! Just the part that "new" isnt necessary is confusing for me now. But I'll look into it thanks! – MaxMadman Nov 19 '16 at 03:34