About C / C ++ Stack Distribution

When learning C ++ (and C), I had some special doubts about the work of stack distribution, that I cannot find a solution for:

  • Does the malloc / free stack allocation allocate functions implicitly? If not; How does it ensure that there is no conflict between stack allocation and heap allocation?

  • If yes; Does stacks in C ++ implicitly call new / delete too? If yes; Does overloading a new statement for a class affect its stack distribution?

This gave confusing results in VC ++; but since VC ++ is not quite standard (or as I heard), I decided it was better to ask here ...

+4
c ++ c memory-management stack heap
Mar 26 '09 at 12:40
source share
7 answers

Stack allocation does not use anything like malloc / free. It uses a piece of memory called the software stack, which is just an adjacent memory segment.

There is a special register that stores the top of the stack. When a new object is created on the stack, the vertex rises, thereby increasing the stack, when the object is freed (goes beyond), the top is lowered from above, thereby reducing the stack.

If you try to allocate too large an object on the stack or go too deep into recursion, the vertex will overlap the maximum allowable stack size, and this is called stack overflow.

Note: the actual direction of stack growth (increasing or decreasing addresses) will vary depending on the system , but the general idea remains the same regardless of the actual direction.

+23
Mar 26 '09 at 12:44
source share

The answer to your first question: None. The stack does not stand out from the heap at all.

First you must read What and where is the stack and heap in order to understand the basic concepts.

+15
Mar 26 '09 at 12:42
source share

Stack distribution is usually done using alloca () or implicitly by the compiler. A well-executed alloca () will require only a small number of instructions, and there is no free (or even necessary) to release it when you are done.

You can pass a pointer to the memory allocated by alloca () to any other function / method that the pointer expects. You SHOULD NOT return the pointer allocated by alloca ().

Here are a few advantages and disadvantages for using stack distribution.

+6
Mar 26 '09 at 12:47
source share

In C and C ++, there are two types of memory allocation: "automatic", where the object is created for the lifetime of the function call and "dynamic", where some memory is allocated by the function provided by the runtime.

In the vast majority of runtime implementations, automatic objects are allocated using the continuous stack provided by the operating system when creating the stream. The stack usually starts with a high-value address and shrinks in size. Dynamic allocations (malloc in C, new in C ++) use some other memory requested by the operating system. Since the OS knows about the addresses that the stack uses, it does not allocate the same addresses to dynamic requests. Since the dynamic region is not ordered, it is often called a heap.

Thus, the distribution of "stack" is not malloc / free. Automatic objects in C ++ invoke the constructor and destructor, but not new or deleted, as new and deleted also have code for managing dynamic memory.

+5
Mar 26 '09 at 12:55
source share

There is a good question here:

"how does he assure that there is no conflict between stack allocation and heap allocation?"

In almost all C / C ++ implementations, there is one contiguous address space, so the allocated stack and heap memory must coexist in this space.

Although every time the stack grows and shrinks, it does not work with separate heap allocations, you can still think of the stack as one large block of memory allocated from the heap. If the stack grows outside this block, then we have a stack overflow (catchy name ... someone should name the site after it).

In a multi-threaded program, every time a thread starts, a new stack must be assigned to it, and when the thread dies, the stack can be freed. And it would be advisable that these blocks of the whole package be distributed using the same heap control as through malloc / free .

So - very roughly speaking - you can think of the stack as the type of object that coexists on the heap. The whole malloc -ed stack is all in one go when the thread starts, and then it gets allocated from it, and then it gets free -d at a time.

On Windows, you can (if you like living life dangerously) call the same virtual memory APIs to find out about the stack and make the virtual page free in it.

+5
Mar 26 '09 at 13:31
source share

No, stack allocation does not call malloc / free. All stack space is allocated at the beginning of your program. When you enter each function, the stack pointer advances enough to provide stack space for the "stack frame" in which your allocated stack variables will be located.

+1
Mar 26 '09 at 12:44
source share

Kepp remembers that "stack allocation" is an implementation detail. There is no guarantee that the stack is used for automatic storage. For example, IBM mainframes did not give (although they tell me that their more modern machines do).

+1
Mar 26 '09 at 14:09
source share



All Articles