This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SCOPE STACK OVERFLOW

HI, i am writing a program in which there are many do-while loops one inside the other...
when i try compile this program... it gives me a error stating.."'{' scope stack overflow.."
it point to braces i have used in DO-WHILE an IF statement...
plss help me to solve this problem...
THANKS...

Parents
  • You have done something wrong with your algorithm if you need 31 nested loops. Exactly what problem are you trying to solve that has so many nested loops?

    When solving a problem that have an almost "unbound" number of variables and the algorithm should scan the full solution space, you don't do it by nesting loops. You do it by using data structures for the individual variables.

    Think about a watch. It has hours and minutes and seconds. It loops 0..23 hours. Inside that it loops 0..59 hours. Inside that it loops 0..59 seconds. Just that there are not 3 nested loops. The code just looks like:

    if (++seconds >= 60) {
        seconds = 0;
        if (++minutes >= 60) {
            minutes = 0;
            if (++hours >= 24) {
                hours = 0;
            }
        }
    }
    


    Above code still shows nesting - but as you can see, the code can be trivially rewritten:

    overflow = false;
    if (++seconds >= 60) {
        seconds = 0;
        overflow = TRUE;
    }
    if (overflow) {
        overflow = FALSE;
        if (++minutes >= 60) {
            minutes = 0;
            overflow = TRUE;
        }
    }
    if (overflow) {
        overflow = FALSE;
        if (++hours >= 24) {
            hours = 0;
            overflow = TRUE;
        }
    }
    if (overflow) {
        overflow = FALSE;
        if (++wdays >= 7) {
            wdays = 0;
            overflow = TRUE;
        }
    }
    if (overflow) {
        overflow = FALSE;
        ...
    }
    

    Suddenly, an infinite number of loops can be handled in sequence without nesting.

    Obviously, the above code, if handling a really silly number of loop variables, can be rewritten even more, where there is just a table of n variables and n overflow + reset values, so you have:

    #define NELEM(a) (sizeof(a)/sizeof(*(a)))
    struct {
        unsigned val,restart,overflow;
    } variables[] = {
        {0,0,60},  // seconds
        {0,0,60},  // minutes
        {0,0,24},  // hours
        {0,0,7},   // wdays
    };
    unsigned i;
    
    for (i = 0; i < NELEM(variables); i++) {
        if (++variables[i].val < variables[i].overflow) {
            // No overflow, so no carry to next level of variables.
            break;
        } else {
            variables[i].val = variables[i].restart;
        }
    }
    

Reply
  • You have done something wrong with your algorithm if you need 31 nested loops. Exactly what problem are you trying to solve that has so many nested loops?

    When solving a problem that have an almost "unbound" number of variables and the algorithm should scan the full solution space, you don't do it by nesting loops. You do it by using data structures for the individual variables.

    Think about a watch. It has hours and minutes and seconds. It loops 0..23 hours. Inside that it loops 0..59 hours. Inside that it loops 0..59 seconds. Just that there are not 3 nested loops. The code just looks like:

    if (++seconds >= 60) {
        seconds = 0;
        if (++minutes >= 60) {
            minutes = 0;
            if (++hours >= 24) {
                hours = 0;
            }
        }
    }
    


    Above code still shows nesting - but as you can see, the code can be trivially rewritten:

    overflow = false;
    if (++seconds >= 60) {
        seconds = 0;
        overflow = TRUE;
    }
    if (overflow) {
        overflow = FALSE;
        if (++minutes >= 60) {
            minutes = 0;
            overflow = TRUE;
        }
    }
    if (overflow) {
        overflow = FALSE;
        if (++hours >= 24) {
            hours = 0;
            overflow = TRUE;
        }
    }
    if (overflow) {
        overflow = FALSE;
        if (++wdays >= 7) {
            wdays = 0;
            overflow = TRUE;
        }
    }
    if (overflow) {
        overflow = FALSE;
        ...
    }
    

    Suddenly, an infinite number of loops can be handled in sequence without nesting.

    Obviously, the above code, if handling a really silly number of loop variables, can be rewritten even more, where there is just a table of n variables and n overflow + reset values, so you have:

    #define NELEM(a) (sizeof(a)/sizeof(*(a)))
    struct {
        unsigned val,restart,overflow;
    } variables[] = {
        {0,0,60},  // seconds
        {0,0,60},  // minutes
        {0,0,24},  // hours
        {0,0,7},   // wdays
    };
    unsigned i;
    
    for (i = 0; i < NELEM(variables); i++) {
        if (++variables[i].val < variables[i].overflow) {
            // No overflow, so no carry to next level of variables.
            break;
        } else {
            variables[i].val = variables[i].restart;
        }
    }
    

Children