While studying C++ (and C) I had some particular doubts regarding the working of stack allocation, that I can't find a solution to:
Does stack allocation call malloc/free functions implicitly? If not; how does it assure there is no conflict between stack allocation and heap allocation?
If yes; does stack allocation in C++ implicitly call new/delete too? If yes; does overloading the new operator for a class affect its stack allocation?
It yielded confusing results in VC++; but as VC++ isn't entirely standard-compliant (or so I heard) I decided I better ask here...
The answer to your first question is No. Stack is not allocated from the heap at all.
You should read What and where are the stack and heap first to understand the basic concepts.
No, stack allocation does not call malloc/free. The entire stack space is allocated at the start of your program. On entry to each function the stack pointer is advanced sufficiently to allow space on the stack for a "stack frame" which will be where your stack allocated variables reside.
In C and C++, there are two types of memory allocation 'automatic', where the object is created for the lifetime of a function call, and 'dynamic', where some memory is allocated by a function provided by the runtime.
In the vast majority of runtime implementations, automatic objects are allocated using a contiguous stack provided by the operating system when the thread is created. The stack typically starts at a high valued address, and is decremented by the size of the object. Dynamic allocations (malloc in C, new in C++) use some other memory requested from the operating system. As the OS knows about the addresses the stack is using, it doesn't allocate the same addresses to the dynamic requests. As the dynamic area is not ordered, it is often called the heap.
So 'stack' allocation doesn't malloc/free. Automatic objects in C++ call the constructor and destructor, but not new or delete, as new and delete also have the code to manage dynamic memory.
Stack allocation is typically done in terms of alloca() or implicitly by the compiler. A well-done alloca() will only require a scant few instructions, and there is no cost (or even a need) to free it when you're done.
You can pass a pointer to memory allocated by alloca() to any other function/method that expects a pointer. You MUST NEVER return a pointer allocated by alloca().
Here are some advantages and disadvantages to using stack allocation.
Kepp in mind that "stack allocation" is an implementation detail. There is no guarantee that a stack is used for automatic storage. For example, IBM mainframes of lore didn't (though I'm told their more modern machines do).
Stack allocation doesn't use anything like malloc/free. It uses a piece of memory called program stack which is just a contiguous segment of memory.
There's a special register that stores the top of the stack. When a new object is created on stack the top is raised thus increasing the stack, when an object is deallocated (goes out of scope) the top is lowered thus decreasing the stack.
If you try to allocate a too large object on stack or go too deep into recursion the top will outgrow the maximum allowed size of the stack and this is called stack overflow.
Note: actual direction of stack growth (increasing or decreasing addresses) will vary by system, but general idea is the same regardless of actual direction.