Code examples: Stack Vs Heap.
Initially (ptr) does not have an address stored inside it, because we initialized it to nullptr after it was declared. It is stored on the stack, the address (ptr) at that time has an address that is part of the addressing of the stack memory. Now we assign the address (a) - (ptr); (ptr) is still on the stack, (a) is on the stack, and address (a) is stored on (ptr).
As soon as we set (ptr) = new SomeClass (), at this moment the compiler allocates memory with the size of this class object and stores a random address from the heap in (ptr). (ptr) is still on the stack of this function, but the memory address that is now stored (ptr) is from the heap. Content (ptr) is accessible outside the scope of this function, but must be returned either through this type of return value, or by one of its parameters by reference.
In other words, the address (ptr) does not change in this function, it remains the same because the variable (ptr) itself still lives on the stack, it is that this pointer variable has a value that changes from the memory address of the stack to the address heap memory when a new one is used.
To demonstrate this run of this code in main () and see the results; this still applies to other functions and objects of the class.
#include <iostream> int main() { int a = 10; int* ptr = nullptr; std::cout << "Memory Address that ptr is pointing to: " << ptr << std::endl; std::cout << "Memory Address of ptr: " << &ptr << std::endl; // std::cout << "What is stored in the variable at this memory address: " << *ptr << std::endl; // This can not be called program will crash, // ptr can not be dereferenced for it is not pointing to anything std::cout << std::endl; ptr = &a; std::cout << "Memory Address that ptr is pointing to: " << ptr << std::endl; std::cout << "Memory Address of ptr: " << &ptr << std::endl; std::cout << "What is stored in the variable at this memory address: " << *ptr << std::endl; std::cout << std::endl; ptr = new int; *ptr = 12; std::cout << "Memory Address that ptr is pointing to: " << ptr << std::endl; std::cout << "Memory Address of ptr: " << &ptr << std::endl; std::cout << "What is stored in the variable at this memory address: " << *ptr << std::endl; std::cout << std::endl; delete ptr; return 0; }
As you can see, the memory address (ptr) does not change, which changes the memory address that is stored internally (ptr). Now the address (ptr) may change if you use the new one on another pointer that points to (ptr).
Now, when you call new on a class type and you store the heap memory address in a pointer of this type, the memory address is where the class object lives, so in this case everything that belongs to this class is on the heap.
Globals and static variables:
Globals - globals are listed in (program file namespace) * and are available anywhere in the declared file and have an application lifetime.
Static local variables are similar to global variables, except that they are available only in the declared function and are initialized only once during the execution of the application. They have an application lifetime.
Static member variables for a class behave like regular static variables, but they do not have (this) pointer associated with them. They are visible to the function that declares them, they are initialized once for the execution time of the application, and they have the lifetime of either the execution time of the application or until there are no more instances of this class object.
(*) I donโt know the technical name, but using the file namespace of the program makes sense according to how the global variables and their memory behave. - I rarely use global variables!
Here are the links for the links.