richard
richard

Reputation: 309

C: When to use stack allocated array and when to use heap allocated array

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?

Upvotes: 4

Views: 2755

Answers (5)

Greg
Greg

Reputation: 1660

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.

Upvotes: 0

rashok
rashok

Reputation: 13424

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.

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222679

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.

Upvotes: 1

Paul Irofti
Paul Irofti

Reputation: 432

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.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409176

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.

Upvotes: 3

Related Questions