1

I'm aware that questions about the stack vs. the heap have been asked several times, but I'm confused about one small aspect of choosing how to declare objects in C++.

I understand that the heap--accessed with the "new" operator--is used for dynamic memory allocation. According to an answer to another question on Stack Overflow, "the heap is for storage of data where the lifetime of the storage cannot be determined ahead of time". The stack is faster than the heap, and seems to be used for variables of local scope, i.e., the variables are automatically deleted when the relevant section of code is completed. The stack also has a relatively limited amount of available space.

In my case, I know prior to runtime that I will need an array of pointers to exactly 500 objects of a particular class, and I know I will need to store the pointers and the objects throughout the duration of runtime. The heap doesn't make sense because I know beforehand how long I will need the memory and I know exactly how man objects I will need. The stack also doesn't make sense if it is limited in scope; plus, I don't know if it can actually hold all of my objects/pointers.

What would be the best way to approach this situation and why? Thanks!

trincot
  • 317,000
  • 35
  • 244
  • 286
Anthony
  • 8,570
  • 3
  • 38
  • 46
  • 3
    Talking about all this "stack" and "heap" nonsense has blinded you completely to the existence of static storage, which is where your data should probably go. Think "static, dynamic, automatic" when it comes to storage classes, and "permanent, manual, automatic" for the corresponding object lifetimes (approximately). – Kerrek SB Feb 09 '12 at 19:42
  • 1
    Word, Kerrek. I really wish the whole stack/heap terminology would just die its horrible death and leave us forever... – Blindy Feb 09 '12 at 19:50

7 Answers7

5

Objects allocated on the stack in main() have a lifetime of the entire run of the program, so that's an option. An array of 500 pointers is either 2000 or 4000 bytes depending on whether your pointers are 32 or 64 bits wide -- if you were programming in an environment whose stack limit was that small, you would know it (such environments do exist: for instance, kernel mode stacks are often 8192 bytes or smaller in total) so I wouldn't hesitate to put the array there.

Depending on how big your objects are, it might also be reasonable to put them on the stack -- the typical stack limit in user space nowadays is order of 8 megabytes, which is not so large that you can totally ignore it, but is not peanuts, either.

If they are too big for the stack, I would seriously consider making a global variable that was an array of the objects themselves. The major downside of this is you can't control precisely when they are initialized. If the objects have nontrivial constructors this is very likely to be a problem. An alternative is to allocate storage for the objects as a global variable, initialize them at the appropriate point within main using placement new, and explicitly call their destructors on the way out. This requires care in the presence of exceptions; I'd write a one-off RAII class that encapsulated the job.

zwol
  • 135,547
  • 38
  • 252
  • 361
4

It is not a matter of stack or heap (which to be accurate do not mean what you think in c++: they are just data structures like vector, set or queue). It is a matter of storage duration.

You most likely need here static duration objects, which can be either global, or members of a class. Automatic variables declared inside the main function could also do the job, if you design a way to access them from your other code.

There is some information about the different storage durations of C++ (automatic, static, dynamic) there. The accepted answer however uses the confusing stack/heap terminology, but the explanation is correct.

Community
  • 1
  • 1
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
3

the heap is for storage of data where the lifetime of the storage cannot be determined ahead of time

While that is correct, it's also incomplete.

The stack unwinds when you exit its scope, so using it for global scoped variables is unfeasible, like you said. This however is where you stop being on the right track. While you know the lifetime (or more accurately the scope since that's the most important factor here), you also know it's above the stack frame, so given only two choices, you put it on the heap.

There is a third option, an actual static variable declared at the top scope, but this will only work if your objects have default constructors.

TL;DR: use global (static) storage for either a pointer to the array (dynamic allocation) or just the actual array (static allocation).

Note: Your assumption that the stack is somehow "faster" than the heap is wrong, they're both backed in the same RAM, you just access it relative to different registers. Also I'd like to mention yet again how much I dislike the use of the terms stack and heap.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Static/global variables need not have default constructors, and they may be either statically or dynamically initialized, depending on whether the initializer is a constant expression (roughly). – Kerrek SB Feb 09 '12 at 19:50
  • He's talking about arrays of objects though, and those *do* have to have default constructors. – Blindy Feb 09 '12 at 19:51
  • *Allocation* on the stack is most definitely faster than the heap, often by orders of magnitude. But this is not relevant for objects that live for the entire runtime of the program. – zwol Feb 09 '12 at 19:51
  • He never mentioned allocation in his claim though. You're making assumptions as much as he is... – Blindy Feb 09 '12 at 19:52
  • @Blindy: Arrays can be list-initialized, which I believe does not require a default-constructor, either. – Kerrek SB Feb 09 '12 at 20:01
  • @Zack - That depends on your heap implementation, but you are correct in most cases. – BigSandwich Feb 09 '12 at 20:05
  • @BigSandwich Can you point me at a counterexample? If stack allocation isn't blazingly fast, that hurts *every function call*, and `malloc` has quite a lot of work to do... – zwol Feb 09 '12 at 20:09
  • @Zack, There's no requirement at all that the system you're programming on even has a stack you know, which is why the names used are incorrect. You can easily simulate what you call stack with a "heap" only system -- I've done a C compiler for a machine like this and it worked fine. – Blindy Feb 09 '12 at 20:14
  • @Zack, don't use malloc :) Allocate a chunk of memory from the OS up front and then write your own algorithm to allocate it. Override new and delete to allocate from your own heap. You can make specialized heaps for certain types of data. – BigSandwich Feb 09 '12 at 22:42
  • In my particular case I'm not concerned about allocation, but about access speed. Thanks for you answer, very helpful. – Anthony Feb 10 '12 at 04:42
2

The stack, as you mention, often has size limits. That leaves you with two choices - dynamically allocate your objects, or make them global. The time cost to allocate all of your objects once at application startup is almost certainly not of significant concern. So just pick whichever method you prefer and do it.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
2

I think you are confusing the use of the stack (storage for local variables and parameters) and unscoped data (static class variables and data allocated via new or malloc). One appropriate solution based on your description would be a static class that has your array of pointers declared as a static class member. This would be allocated on a heap like structure (maybe even the heap depending on your C++ implementation). a "quick and dirty" solution would be to declare the array as a static variable (basically a global variable), however it isn't the best approach from a maintainability perspective.

1

The best concept to go by is "Use the stack when you can and the heap when you must." I don't see why the stack wouldn't be able to hold all of your objects unless they're large or you're working with a limited resources system. Try the stack and if it can't handle it, the time it would take to allocate the memory on the heap can all be done early in the program's execution and wouldn't be a significant problem.

Foggzie
  • 9,691
  • 1
  • 31
  • 48
0

Unless speed is an issue or you can't afford the overhead, you should stick the objects in a std::vector. If copy semantics aren't defined for the objects, you should use a std::vector of std::shared_ptrs.

Peter O.
  • 32,158
  • 14
  • 82
  • 96