Are variables local to the class that are not declared as pointers created on the heap or stack?

Take the following:

A a; class B; // No content for brevity class A { public: A() { b.SetTitle("hi"); } private: B b; } int main() { return 0; } 

The question here is that b declared inside A is declared on the heap or on the stack.

If on the heap does this mean that it is automatically deleted or should I also delete it?

Side question:

This is what I originally did, but thought I was a little stupid, because I had to continue to announce everything as new everywhere. If the above is on the stack, I think it wasnโ€™t so stupid right?

 A a; class B; // No content for brevity class A { public: A() { this->b( new B() ); // I don't have C++ 14 :( so i can't do make_unique b->SetTitle("hi"); } private: unique_ptr<B> b; } int main() { return 0; } 
+7
c ++
source share
2 answers

It can still be either or even (as Thomas Matthews points out below) in a fixed block of memory. In this case, you can think of it as "inside A". If you put A on a pile in a pile. If you select "A" at the top of the function, say, on the stack (but inside A).

It is written so that it is part of A, and its lifetime is tied to A. You only need to worry about managing A.

+10
source share

Code examples: Stack Vs Heap.

 // Not a proper class but used for demonstration purposes class SomeClass { private: int x; int* ptr; public: SomeClass() { // Both On x and ptr are on the stack when this constructor is defined and used. x = 10; ptr = &x; } SomeClass() { // x is on the stack but ptr is on the heap and delete needs to be called to release the memory. x = 10; ptr = new int; } }; void someOtherFunction() { SomeClass a; // on the stack relative to this function SomeClass* ptr = nullptr; // ptr is on the stack and is initialize to null. ptr = &a; // Still on the stack and ptr stores the address of (a). ptr = new SomeClass(); // Now ptr lives on the heap and delete needs to be called to release its memory. } 

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.


0
source share

All Articles