1

I have a few questions regarding dynamic variables and pointers so I can understand them better.

Are dynamic variables automatically disposed of when their scope expires? If not, then what happens?

Is the value of a pointer a memory address? If not, then what are they?

  • 1
    Out of curiosity: Are these your questions or quiz/exam questions that you want the answer to? – Nathan Pierson May 18 '21 at 00:37
  • No, they're questions I have in my mind right now because it wasn't covered in the material I was learning about pointers and dynamic variables/arrays. – EternalNightmare01 May 18 '21 at 00:39
  • Raw pointers are just POD types (plain old data) and are not destructed. That means you must clean up yourself (by calling the corresponding function to deallocate). If you're talking about _smart_ pointers (_e.g._ `std::unique_ptr`, `std::shared_ptr`), then yes _those_ are managed properly when the last reference to the managed object goes out of scope. Yes, a pointer is a memory address, but it might not correspond to a physical memory address -- that depends on your operating system and computer architecture. – paddy May 18 '21 at 00:40
  • 1
    If you create an object with a dynamic lifetime (such as with `new`) nothing special happens when the last pointer goes out of scope. The object keeps existing in limbo where nothing can ever access it again. You need to tell the program explicitly when you are done with it (`delete` in the case of `new`). Typically, you would use `std::make_unique` instead, which returns an instance of `std::unique_ptr`, a class type that acts like a pointer, but always deletes the owned object when it goes out of scope. – François Andrieux May 18 '21 at 00:44
  • The value of a pointer is the identity of some instance of an object of the pointed type. On basically all implementations, this is done by storing a memory address where that object is stored. But an implementation could choose a different system if it can figure out another system that satisfies all the rules of the language. – François Andrieux May 18 '21 at 00:46
  • Handy reading that should clear a few things up: [Why are the terms “automatic” and “dynamic” preferred over the terms “stack” and “heap” in C++ memory management?](https://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and) – user4581301 May 18 '21 at 00:54

1 Answers1

1

It's important to understand that there two complete separate, discreet, independent "things" that you are asking about.

  1. A pointer to an object that was created in dynamic scope (i.e. with the new statement).

  2. And the object itself, that the pointer is pointing to.

It's important for you two separate the two in your mind, and consider them as independent entities, in of themselves. Insofar as the actual pointer itself, its lifetime and scope are no different than any other object's. When it goes out of scope it gets destroyed.

But this has no effect on the object the pointer was pointing to. Only delete destroys the object. If there's some other pointer, that's still in scope, that points to the same object, you can use that pointer for the requisite delete.

Otherwise you end up with a memory leak.

And the value of the pointer is, for all practical purposes, a memory address. This is not specified in any form or fashion in the C++ standard, which defines pointers and objects in dynamic scope in terms of how their behavior is specified. However on all run-of-the-mill operating systems you'll see a memory address in the pointer.

It's also important to understand that since a pointer is an independent object, a pointer doesn't have to point to an object in dynamic scope. There are many pointers that don't. It's not a trivial task to keep track of all objects, all pointers, and to figure out which ones need to be deleted properly. Modern C++ has additional classes and templates that will help you do that, which you'll learn about in due time.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148