About CC stack allocation

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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...

Answers

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.

http://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems http://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/685601/about-c-c-stack-allocation

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils