Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of C Block Scope in Interrupt Handler?

Tags:

c

embedded

cpu

arm

Is there any advantage or disadvantage of using a C block scope in a function or specifically inside an Interrupt Handler?

From the link - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472i/CJAIIDCG.html - Refer stack usage.

In general, you can lower the stack requirements of your program by:

  1. Writing small functions that only require a small number of variables.
  2. Avoiding the use of large local structures or arrays.
  3. Avoiding recursion, for example, by using an alternative algorithm.
  4. Minimizing the number of variables that are in use at any given time at each p oint in a function.
  5. Using C block scope and declaring variables only where they are required, so overlapping the memory used by distinct scopes.

Advantage or disadvantage in using the C block Scope is not very clear.

like image 363
pdssn Avatar asked Nov 25 '25 17:11

pdssn


1 Answers

If you write:

{
    int foo[1000];
    int bar[1000];
    … code that uses foo …
    … code that uses bar …
}

then foo and bar exist for the entire block and must use different memory. (The compiler/optimizer may recogize they are not used simultaneously and arrange to use the same memory, but various things can interfere with this, so you cannot rely on it.)

If you write:

{
    {
        int foo[1000];
        … code that uses foo …
    }
    {
        int bar[1000];
        … code that uses bar …
    }
}

Then foo and bar only exist at different times, so the compiler can use the same memory for them.

like image 149
Eric Postpischil Avatar answered Nov 28 '25 15:11

Eric Postpischil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!