What is a stack imbalance?
Question: Having read this article [F# Versus Mathematics: Part One - Getting Started
with BLAS and LAPACK](http://www.codeproject.com/KB/net-
languages/FSharpvsmathematicspt01.aspx) I stumbled across the term `stack
imbalance` in the paragraph `A Warning, Perhaps an Omen`.

I
[googled](http://www.google.de/search?ie=UTF-8&q=what%20is%20a%20stack%20imbalance)
and searched on
[SO](http://stackoverflow.com/search?q=%22stack%20imbalance%22), but could
only find people struggling with stack imbalances and no generall
explanations.

**Bonus Question:** Does it only affect f# or is it a general problem in C,
C++, Python, Java, etc.?

p.s. please change the tags of the question if necessary


Answer: A stack imbalance occurs when the data structure used to keep track of called
functions, arguments, and return values becomes corrupted or misaligned.

Most times, the stack is a memory pointer that stores the address where
control will resume when the current function call exits back to the caller.
There are different variants on this, sometimes the arguments to a function
are also appended to the stack, as well as the return value. What is most
important here is that the caller and callee should agree upon how to restore
it back to the prior state when the callee exits. This agreement is frequently
known as the _Calling Convention_.

In .NET, stack imbalances are a rare to nonexistent problem in pure managed
code. However, this can be a frequent problem when calling unmanaged code, as
you will need to tell the compiler how the method should be called, which then
implies how the stack should be cleaned up per the calling convention.

On windows, there are a few standard calling conventions that cover the bulk
of invocation cases.

**stdcall** \- Callee will fix the stack upon exit.  
**fastcall** \- Potentially no need to fix the stack aside from return
address, instead, CPU registers are used to pass arguments.  
**cdecl** \- The caller will fix the stack after the called function returns.

A formal reference is available here: [Argument Passing and Naming Conventions
@ MSDN](http://msdn.microsoft.com/en-us/library/984x0h58.aspx)

This is also of interest: [X86 calling convention list @
Wikipedia](http://en.wikipedia.org/wiki/X86_calling_conventions)

Within a given development domain, this tends not to be a problem. Each
language generally has a convention that is implicit for all method calls.
C/C++ uses the same convention for invocation of C/C++ calls, Python for other
Python calls, etc. When crossing domains, it can become a problem if one
domain doesn't use the same as another. Perhaps most common in windows, a
function exported with "C" style declarations (cdecl) may cause an unbalanced
stack (or worse) when called as though it had a stdcall convention, which is
the method recognized by WINAPI (windows system) calls.

