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.

Issue with global variable (Compilator Bug?)



Here below I illustrated the issue I encounter with my code.

I declare a global variable(buffer[][]), then I use it as an argument in the Read() function, this function is being called by another function : Tot()
When the global variable is being passed as an argument something is going wrong and the global variable seem to not exist in the local context of a function called in another functon when not passed as an argument.

To summarize there is a bug in the handling of global variables they are not usable as such in some context ...

I am using CCStudio  Version: 5.3.0.00090  and the compiler TI v4.1.4 and debugging on EXP430FR5739

EXAMPLE to illustrate the issue encountered.
#include <msp430.h>
/*
* main.c
*/
void Read(char* );
void Tot();
static char buffer[5][10];
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
unsigned i,j;
for(i=0;i<5;i++)
for(j=0;j<10;j++)
buffer[i][j]=j+i*5;
Tot();
return 0;
}
void Read(char* Sentence)  //In this function the argument Sentence is being messed up and though there is no compilation error it is not working
{
int Total;
unsigned i;
for(i=0;i<10;i++)
Total+=Sentence[i];
}
void Tot()
{
Read((char *)&(buffer[0][0]));
}
  • I don't understand the nature of the problem.  Please describe exactly how you know this is a bug.  In what way is Sentence "messed up?"  In what way is buffer "not usable? "  What did you expect to see, and how did the behavior of the program differ from that?

    Please include your complete command-line options.  We may also need to see the linker map file (--map_file option)

  • What you are observing may be an effect of optimization. If I understand correctly you are saying that you do not see the address of buffer getting passed during the call to Read function. I created a project with this code and stepped through it in the CCS debugger. Within the Read fuction the value of Total is modified but since it is not being used anywhere it is getting optimized out.

    Try rebuilding the project with optimization set to Off (by default some optimization is turned on by the compiler). You can do this from Project Properties->Build->MSP430 Compiler->Optimization, and set --opt_level to off. Let us know if this "shows" you the behavior you were expecting in the debugger.

  • Hi

       I'm trying to get some experience with my LaunchPad with M430G2553, so pasted the code in CCS, turned optimization 'off' per your suggestion

    and changed one line of code in 'void Read ()' where the OP appears to overrun the buffer:

    //for(i=0;i<10;i++)
     for(i=0;i<5;i++)

    and looked with the degugger at 2 values: buffer[1][1] = 6 and buffer[4][1] = 21 which are what I expected. Well the first run gave me those values.

    But on new attempts to run, the debugger says: Error: identifier not found: buffer at (...

    Also a tab saying '0xc146' appears in the editor. When I click on it to open it up, it says "No Source Available for 0xc146"

    What does "No Source Available for 0xc146" mean?

  • To re-explain the problem :

    How I described the problem is When I run step by step the code and I check the values of the different variables what I notice is that :

    buffer (pointer to the first slot of the table)  is not being transfered to char* Sentence in the Read() function.

    instead of the memory adresss of the buffer being used there is some number which I've discovered to be proportionnal to the size of the buffer... weird

  • I also tested the compilation with --opt_level set to off and it worked as expected. So this must be an unexpected optimisation of the code....

    Why is the address not getting passed during the call of the function ? How is that an optimisation of any kind ?

  • Buffer is not being overflowed because the variable being passed can be considered as a single row of the table with the 10 slots.

    So the iteration of the for() loop is done on these 10 slots.

    Besides I don't understand the 0xC146 problem ?

  • Fredoo.net said:

    Buffer is not being overflowed because the variable being passed can be considered as a single row of the table with the 10 slots.

    So the iteration of the for() loop is done on these 10 slots.

    Yes, I see now. I got mixed up on the 'for' loops.

    After thinking about it, I believe you could also do (in your Read () function):

    for(i=0,j=0;i<50;i++)
     Total+= Sentence[i];

    since the array should be holding consecutive data anyway.

  • I mean:

    for(i=0;i<50;i++)
     Total+= Sentence[i];

    I didn't mean to leave the unnecessary j = 0 in the for loop.