4

I've been told not to use stack allocated arrays because the stack is a precious resource. Other people have suggested to me that in fact it is perfectly fine to use stack allocated arrays so long as the array is relatively small.

I would like to have a general rule of thumb: when should I use a stack allocated array? And when should I use a heap allocated array?

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
richard
  • 309
  • 2
  • 8
  • 4
    Use the stack when you can and the heap when you have to. "Stack is a precious resource" == rubbish. Of course it's not a good idea to allocate multimegabyte structures on the stack, but that's quite an outlying case. – Jon Aug 17 '12 at 11:02
  • possible duplicate of [Malloc or normal array definition?](http://stackoverflow.com/questions/11241014/malloc-or-normal-array-definition) – hmjd Aug 17 '12 at 11:24
  • 1
    i really like the phrase 'use the stack when you can and the heap when you have to'! – Rango Aug 17 '12 at 11:40

5 Answers5

3

While all of your memory is limited, even today with enormous amounts of RAM and virtual memory, there is still a limit. However, it's rather large, especially compared with the stack which can be anything from a couple of kb on small embedded systems to a couple of megabytes on a PC.

Besides that, there is also the question about how you are using it, and for what. For example, if you want to return an "array" from a function, it should never be on the stack.

In general, I would say that try to keep arrays on the stack small if you can. If you are creating an array with thousands of entries on the stack you should stop and think about what you want it for.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Buffers will typically have thousands of entries and are commonly allocated on the stack. When you say thousands do you mean tens or hundreds of thousands? – richard Aug 17 '12 at 11:37
  • @richard Well, I've seen code with `char` arrays of size 8192, and even up to 32kb, but in my own opinion above 8k is maybe a little too much. – Some programmer dude Aug 17 '12 at 11:41
  • What is this opinion based on? That is, why is 8 KiB too much, rather than some other limit, such as 10 MiB or 1 GiB? – Eric Postpischil Aug 17 '12 at 12:30
  • @EricPostpischil Considering that even on a modern PC the stack usually is just a few MiB, allocating 10MiB on the stack would be quite bad. The main reason to keep it small would be that you never know how much space you have available and how much will be use further down the road. Sure you can have an array taking up a couple of hundred KiB of stack space, but if the function is called recursive a couple of times then that space is eaten up very quickly. – Some programmer dude Aug 17 '12 at 12:35
2

It depends on your platform.

Nowadays, if working on the popular x64 platform, you don't really have to worry about it.

Depending on the Operating System you use, you can check how much stack space and how much heap space a userland process is allowed to use.

For example, UNIX-like systems have soft and hard limits. Some you can crank up, some you can not.

Bottom line is that you don't usually need to worry about such things. And when you need to know, you are usually tied so closely to the platform you'll be developing for that you know all these details.

Hope I answered your question. If you want specific values please specify your exact hardware, operating system and user privileges.

Paul Irofti
  • 412
  • 3
  • 17
1

The answer to this question is context dependent. When you write for an operating system kernel, for example, the stack might be quite limited, and allocating more than a thousand bytes in a stack frame could cause a problem.

In modern consumer systems, the space available for the stack is typically quite large. One problem systems used to have was that address space was limited and, once the stack was assigned an address, it could not grow any further than the next object in the address space in the direction of stack growth, regardless of the availability of physical memory or of virtual memory elsewhere in the address space. This is less of a problem with today’s address spaces.

Commonly, megabytes of space can be allocated in a stack frame, and doing so is cheap and easy. However, if many routines that allocate large amounts of space are called, or one or a few routines that allocate large amounts of space are called recursively, then problems can occur because too much space is used, running into some limit (such as address space or physical memory).

Of course, running into a physical memory limit will not be alleviated by allocating space from the heap. So only the issue of consuming the address space available for the stack is relevant to the question of whether to use stack or heap.

A simple test for whether this is a problem is to insert use of a great deal of stack space in your main routine. If you use additional stack space and your application still functions under a load that uses large amounts of stack space normally, then, when you remove this artificial reservation in main, you will have plenty of margin.

A better way would be to calculate the maximum your program could use and compare that to the stack space available from the system. But that is rarely easy with today’s software.

If you are running into stack space limits, your linker or your operating system may have options to make more available.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

Scope of Global and static variables will be through out the life of a process. Memory for these variable will be allocated when a process is started and it will be freed only process exits.

But local variable(stack variable) has scope only to a function on which it is defined. Memory will be allocated when a function is invoked and it will be freed once control exits from the function.

Main intention of dynamic memory is to create a variable of user defined scope. If you want to control a scope of variable means, you can allocate memory for a variable x at one function and then pass the reference(address) to as many function you want and then finally you can free it.

So with the help of dynamic allocated memory, we can create a variable which has scope higher than a local variable and lesser than global or static variable.

Apart from this if the size is very very high its better to go for dynamic memroy, if the architecture contains memory constraint.

rashok
  • 12,790
  • 16
  • 88
  • 100
0

The good reason to use heap allocated memory is passing its ownership to some other function/struct. From the other hand, stack gives you memory management for free, you can not forget to deallocate memory from stack, while there is risk of leak if you use heap.

If you create an array just for local usage, the criteria of size of the one to use, however it is hard to give exact size, above which memory should be allocated on heap. One could say that a few hundreds bytes is enough to move to heap, for some others it will be less or more than that.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Greg
  • 1,660
  • 13
  • 12