-2
\$\begingroup\$

I have a strange issue. I want to either increment a value every time the loop function is called or us a random value each time. Whether I use a global int for incrementing or set a local int to a random value I get a strange failure. The code compiles fine but the size is much larger than the compiled size if I define and set to a static value an int in the loop function. Not only does the compiled size increase by 2KB between the two situations, but the execution seems to act erratically.

Examples.

This works

    /********************************************/
    ...
    void setup( void )
    ...
    void loop( void ) {
        int a = 0;
    }
    /********************************************/

This doesn't

    /********************************************/
    int a;
    ...
    void setup( void )
    ...
    void loop( void ) {
        a = 0;
    }
    /********************************************/

Nor does this

    /********************************************/
    ...
    void setup( void )
    ...
    void loop( void ) {
        int a = random( 10 );
    }
    /********************************************/

I would love to see if anyone could explain why this is acting so strangely.

Let me know if you want more code examples

\$\endgroup\$
  • 1
    \$\begingroup\$ Define "works" and "doesn't work" \$\endgroup\$ – Andrew Nov 20 '12 at 7:48
  • 1
    \$\begingroup\$ Is that the entire code? Is it for an Arduino? In the first example I imagine the whole code is optimised away to nothing. \$\endgroup\$ – geometrikal Nov 20 '12 at 11:20
1
\$\begingroup\$

It sounds like your linker is not set up correctly. The allocation for the global will be in unused RAM, whereas the local will be on the stack. I would guess that your linker is set to overlap unused RAM with something else causing the effects you see.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.