Heap or stack for creating objects? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this program which creates "Computer components" and stores it in a vector and writes the objects to a file.
If i create the objects on the stack and pass the memory address to the vector i get an error
if i create the objects on the heap and pass the pointer to the vector it works just fine "Component is a absract base class" and "CPU is a derived class from Component" Can someone explain why this is?
vector<Component*>components;
CPU x;
CPU*y = new CPU();
components.push_back(&x) // results in debug error
components.push_back(y) // works fine.
writeTofile(components);

By doing this
CPU x;
components.push_back(&x);
you create a local object on the stack and push the address of it to the vector. When the function goes out of scope, your local object is not alive anymore, and its address in the vector is invalid.

Related

C++ when to return pointers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Suppose you have a function that is supposed to return an object, do you generally return a pointer of the object or the object itself? When should you return a pointer/object?
Generally speaking you will probably want to return object references, since that way you are only transferring a copy of the pointer and not a copy of the entire object - especially if the object is large.
My understanding is that you'll only want to return a copy when you're creating an object on the stack within a function - this is because the object will go out of scope when the function returns and will be deleted. Objects created on the heap can be returned by reference but must be deleted later to avoid memory leaks.

How to find the total numbers of object created of my class? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
For example, I have my class employee. I want to keep record how many employees till date have worked for me. I can make static count variable and add 1 in my constructor. But whenever my temporary object will be created when we pass object in parameters or return object of our class it will add for them too.
Static class member is the right way to go. A few things to be careful about:
Make sure you overload all constructors. The ones you don't want to support you should explicitly delete.
Don't forget to decrement in destructor.
If this program of yours is multithreaded then use atomic_uint or provide locking mechanism of your own.

Register storage class memory allocation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
who will decide the memory or cpu register allocation for a register type storage class? Will it be assigned during compilation or during run time?
Memory allocation during run-time is handled by new or the malloc family. The delete and free functions dispose of the memory.
Register allocations are assigned by the compiler at compile time. Register usage is part of the executable code.

Declaring a variable which has a value in c++ or c that does not get destroyed when the program terminates [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I declare a variable or array that contain a value which does not get destroyed when the program terminates in c or c++?
When the process terminates, the kernel releases the resources owned by it. If you want to keep data/information obtained during the runtime of the process, you can use a database or the file system.
if you want to keep data after your process terminates, try storing it in a file.
check this for more:
file handling in C++

What is the correct way to minimize data held on the stack? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Preempt: (if anyone can link helpful articles explaining the stack and heap at a deep level, up to registers, would be much appreciated)
I am new to C++: I am trying to really grasp how memory management works. At this point, I understand any declaration of the type ObjClass obj; to have automatic duration within the scope it's declared in. Yet, ObjClass* obj = new ObjClass(); stores the obj pointer on stack, but assigns it an address of memory on the heap. What I'm wondering about is, in more complex programs, what design pattern is used to prevent stack overflow? I could see the storage on stack quickly exceeding 1mb. Is this achieved by making multiple smaller functions which run, use stack, and then automatically deallocate?
Related Q: As for global variables, I know they are held in "static" storage, yet am unsure how that works in the context of stack/heap. How is their memory allocated, and is there a small limit like the stack? Is heap close to the size of system RAM minus the OS-reserved memory?