Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify if an object should be on the stack or not?

I was looking for a rule of thumb for allocating objects on stack or heap in C++. I have found many discussions here on SO. Many people said, it's about the lifetime of an object. If you need more lifetime than the scope of the function, put it in the heap. That makes perfect sense.

But what made me confusing is, many people said, allocate objects to stack if they are small. If objects are big, put it to heap. But none of them said how to identify an object is big or not?

I have the following questions,

  1. How to identify an object is big or not?
  2. What will be the stack maximum size? Each OS will have different stack size?
  3. I have a wrapper class which wraps vector<string>. It will have around 100 items. Will it make a stack overflow if I allocate this class to a stack? I tried this, but it worked perfectly. Not sure I am doing something wrong.
like image 295
Navaneeth K N Avatar asked Mar 06 '09 02:03

Navaneeth K N


People also ask

Can an object be on the stack?

Unlike Java, instances of classes ("objects") can be allocated on the stack in C++. That is, you do not need to use new to allocate an instance of an object.

Why objects are not stored on stack?

If you allocate an object on the stack, its life time is limited to the current method call. When the method returns, the object is automatically destroyed, together with everything else stored in the stack frame.

Which objects are stored in stack?

Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.

Are objects on the stack or heap?

Static (and thread-local) objects are generally allocated in their own memory regions, neither on the stack nor on the heap. And member variables are allocated wherever the object they belong to is allocated. They have their containing object's storage duration.


1 Answers

Well firstly vectors (and all the STL container classes) always allocate from the heap so you don't have to worry about that. For any container with a variable size it's pretty much impossible to use the stack.

If you think about how stack allocation works (at compile time, basically by incrementing a pointer for each object) then it should be clear that vector memory comes from the heap.

std::vector<int> myInts;
std::string myString;
SomeOther Class;

// this memory must come from the heap, there's no way it
// can now be allocated on the stack since there are other
// objects and these parameters could be variable
myString = "Some String";
myInts.reserve(256);

Unless you are in a recursive function you can place several kilobytes of data on the stack without much worry. Stack-sizes are controlled by the program (not the OS) and the default tends to range from 32kb - 1mb. Most desktop software comes in at the 1mb range.

Individual objects are almost never a concern. In general they will either be small enough for the stack, or will allocate internally from the heap.

If objects are local to a function put them on the stack. If not put them on the heap.

Use the heap for large buffers you allocate for loading/sorting/manipulating data.

like image 112
Andrew Grant Avatar answered Oct 02 '22 04:10

Andrew Grant