C: When to use the stack allocated for the allocated array, and when to use the allocated heap array

I was told not to use stacked distributed arrays, because the stack is a valuable resource. Other people suggested to me that it is actually great to use arrays allocated by the stack while the array is relatively small.

I would like to have a general rule: when should I use a dedicated stack stack? And when should I use the array allocated for the heap?

+4
source share
5 answers

While all your memory is limited, even today with a huge amount of RAM and virtual memory, there is still a limit. Nevertheless, it is quite large, especially compared to the stack, which can be from a few kilobytes on small embedded systems to several megabytes on a PC.

In addition, there is also a question about how you use 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 create an array with thousands of entries on the stack, you should stop and think about what you want.

+3
source

It depends on your platform.

Currently, if you are running on the popular x64 platform, you should not worry about that.

Depending on the operating system you are using, you can check how much stack space and how much heap space is allowed for the user process.

For example, UNIX-like systems have soft and hard restrictions. Some of them you can run, some of them you can not.

The bottom line is that you usually do not need to worry about such things. And when you need to know, you are usually tied so close to the platform that you will develop, because you know all these details.

I hope I answered your question. If you need specific values, specify which hardware, operating system, and user privileges.

+2
source

The answer to this question depends on the context. For example, when you write for the kernel of the operating system, the stack can be quite limited, and allocating more than a thousand bytes on the stack can cause a problem.

In modern consumer systems, the space available for the stack is usually quite large. One of the problematic systems was that the address space was limited, and as soon as the address was assigned to the stack, it could not grow beyond the next object in the address space in the direction of the stack growth, regardless of the availability of physical memory or virtual memory elsewhere in the address space . This is less of a problem with today's address spaces.

Typically, megabytes of space can be allocated on the stack, and doing so cheaply and easily. However, if many subprograms that allocate large amounts of space are called, or one or more subprograms that allocate large amounts of space are called recursively, then problems can arise because too much space is used, resorting to a certain limit (for example, the address space or physical memory).

Of course, work in the limit of physical memory will not be reduced by allocating space from the heap. Thus, only the question of using address space for the stack is relevant to the question of whether to use the stack or the heap.

A simple test of whether this is a problem is to insert a large number of stacks into the main space. If you use extra stack space and your application is still running under load, which usually uses large amounts of stack space, then when you delete this artificial reservation in main , you will have a lot of difference.

It would be best to calculate your maximum program that you can use and compare it with the stack space available on the system. But this is very rare with today's software.

If you use stack space restrictions, your linker or your operating system may have options to make them available.

+1
source

The volume of global and static variables will go through the whole process. The memory for this variable will be allocated when the process starts and only the exit from the process will be freed.

But a local variable (stack variable) has scope only for the function on which it is defined. The memory will be allocated when the function is called, and it will be freed after the control exits this function.

The main intention of dynamic memory is to create a user-area variable. If you want to control the volume of variables, you can allocate memory for variable x by one function, and then pass a link (address) to as many functions as you want, and then, finally, you can free it.

Thus, using dynamic allocated memory, we can create a variable that has a scope above a local variable and smaller than a global or static variable.

In addition, if the size is very high, it is better to use dynamic memroy if the architecture contains a memory limit.

+1
source

A good reason to use memory allocated by the heap is to transfer its ownership to some other / struct function. On the other hand, the stack gives you memory management for free, you cannot forget to free memory from the stack, while there is a risk of leakage if you use a heap.

If you create an array for local use only, the size criteria used, however, it is difficult to give the exact size over which the memory should be allocated on the heap. We can say that several hundred bytes are enough to move to the heap, for some others it will be less or more.

0
source

All Articles