This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

global variable is reset after change

Other Parts Discussed in Thread: MSP-TS430RHA40A, MSP430FR5739

Hi Guys

I am using MSP430FR5739 in code composer studio with msp-ts430rha40a developing board. I notice that the static global I have is always reset to same value. 

for Example:

static char a = 0 ;

static char b = 0;

int main( void ){

    b==1;

    if( a == 0 ){

        foo();

    }

   if( b == 1){

       boo();

   }

}

From what I see from debuger, a is never set to zero, and foo() is not called. b cannot be set to 1 but keep as a random value( like 0x02)

I am totally new to msp430 and CCS, Could anyone help ?

Thanks :)

Harry

  • The first line of main is "b==1;". If you want to set b to 1 then it should be "b=1;"

    I'm not sure what's going on with the other variable/function, it'd be helpful if you would post the definition of function foo.

  • harry han said:
    I am totally new to msp430 and CCS

    No offense, but most probably you are totally new to C programming language either :)

    harry han said:
    Could anyone help ?

    Reading C books will help you. When you are done reading and know at least basics of programming using C language, then come here to talk about msp430 hardware.

  • oops thanks to point out my typo :)

  • llmars said:

    Hi llmars

    To be honest, I dont think you understand the question I have here. 

    And I take your 2nd quote as offense, because from what i am understanding, there is no program level requirement to post on this forum. And since you mentioned reading c books, i have to ask which books you have finish to make you think you are skilled enough to answer for questions :)

    Thanks,

    Harry

  • thanks, the links are very helpful :)

  • harry han said:
    To be honest, I dont think you understand the question I have here. 

    I clearly understood your question. And it had nothing to do with msp430 microcontroller this forum is about. Your question was basic C programming question, not related to any particular piece of hardware at all. That's why I became little sarcastic. Apologies & peace.

    Books.. Can't name any particular one because it was looong time ago, they all are gone. For me when I was learning I was looking for books with lot of examples, snippets you can play with compiling them. I suggest to do some Amazon.com searches, look reviews of most popular books and see wat's more suited for you.

  • Here's some suggested 'C' learning & reference resources - including books:

    http://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/

     

  • hi llmars

    Let me explain the question so you could understand it, because it is not a C problem.....

    char a = 5;                    

    int main ( void ) {

        char b = 0;

        if( a == 5 ){

            b == 1;   

        }

        if( b == 1 ){

            a = 0;

        }

    }

    Free run result:

    a = 0 b = 1

    Single step result:

    a = 6( something changed it in the memory when pc enter main ) b =0 

    so i am asking what changed variable a in the background and how to avoid it.

    llmars, I appropriate your help but i really dont think you understand the question.

    Harry

  • harry han said:
    Let me explain the question so you could understand it, because it is not a C problem.....

    Well, ok, this is not language-related but knowing your compiler, libraries and debugger. Besides (repeated) b variable assignment typing error you are not paying attention to C start-up routine which initializes global variables. Different compilers can use different naming for it, but common name is cstartup() or _cstartup(). This function is execute _before_ main(). When you run debugger in single step mode, most probably you somehow avoid execution of cstartup() or debugger is configured to not run it, a is not initialized in result. You shall check debugger options first, if nothing there then simply look for function, place breakpoint in it, debug.

  • harry han said:
    From what I see from debuger, a is never set to zero, and foo() is not called. b cannot be set to 1 but keep as a random value( like 0x02)

    As robert said, b is never set to 1. The instruction 'b==1' results in a 'false' which is then discarded as it is not used for anything. Likely the compiler has eliminated it completely.
    And sicne oyu declared a as static, the compiler knows that nobody will ever set a to anything else but 0 (static vars are invisible outisde the current source file), so it can be taken as a constant and discarded too. However, foo() should be called (the surrounding if gets a constant 0==0 condition).

    The above main code (assuming that there is no ohter sourcecode in the same C file) can be reduced to

    int main(void){ foo(); }

    Which probably is what the compiler does.

    If a and b weren't static (but global then and visible outisde the current C file) or used somewhereelse in teh same source file, things would be different.

  • harry han said:
    it is not a C problem.....

    Maybe not 'C' specifically, but you still seem to be making false assumptions about compiled, high-level language code - in particular, the likelihood that a compiler will optimise-out stuff that is clearly pointless.

    The chances are that what you're looking at in the debugger is not actually variables 'a' and 'b' - because those variables will have been optimised away.

    harry han said:
    Free run result:

    What does that actually mean? You program produces no result - it exits main (which, in this context, is an error in itself) but does not specify a return value.

    How and at what point do you evaluate/inspect this "result"?

    harry han said:
    Single step result:

    Again, what does that mean? How and at what point do you evaluate/inspect this "result"?

     

**Attention** This is a public forum