Compiler error: Blocks are nested too deeply

Last edited on

Problem

When instrumented, a C/C++ file produces an error like

fatal error C1061: compiler limit: blocks nested too deeply

and the compilation fails.

Explanation

Most of the time, this error is caused by long "else if" chains like this:

if (c1)
    ...
else if (c2)
    ...
else if (cn)
    ...
else if (d1)
    ...

For such "else if" chains, the instrumented code generated by Coco becomes deeply nested. Compilers often have a maximal depth of nesting they can handle, so if the "else if" chain is too long, the compilation of the instrumented code fails.

Solution

To handle this, the chain can be broken up. The following code has the same functionality as the code above, but now consists of two shorter "else if" chains:

int cont = false;
if (c1)
    ...
else if (c2)
    ...
else
    cont = true;

if (cont) {
    if (cn)
        ...
    else if (d1)
        ...
}