Search code examples
c++masm

MASM locals: dynamically allocated data?


I'm learning masm32, following some tutorials. In one tutorial: http://win32assembly.online.fr/tut3.html there is stated:

LOCAL directive allocates memory from the stack for local variables used in the function. The bunch of LOCAL directives must be immediately below the PROC directive. The LOCAL directive is immediately followed by :. So LOCAL wc:WNDCLASSEX tells MASM to allocate memory from the stack the size of WNDCLASSEX structure for the variable named wc. We can refer to wc in our codes without any difficulty involved in stack manipulation. That's really a godsend, I think. The downside is that local variables cannot be used outside the function they're created and will be automatically destroyed when the function returns to the caller. Another drawback is that you cannot initialize local variables automatically because they're just stack memory allocated dynamically when the function is entered . You have to manually assign them with desired values after LOCAL directives.

I've always been told stack memory is static, and any dynamic allocation is heap. Can we really consider those as locals in the sense of C++ then? When you create local variables in C++, will those variables be dynamically allocated on the stack as well?


Solution

  • Can we really consider those as locals in the sense of C++ then? When you create local variables in C++, will those variables be dynamically allocated on the stack as well?

    In C++, local (automatic) variables live on the stack, so yes and yes.

    They are allocated dynamically in the sense that they come and go as the function is entered/exited. However, as you rightly point out, this type of allocation is rather different from heap allocation.

    In addition to the heap and the stack, there is a third area where variables can reside. It is the data segment. It's where global as well as function- and class-level static variables live.