Array Declaration problem

Discussion in 'Programmer's Corner' started by Jswale, Sep 30, 2015.

  1. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    Hi All,

    Probably an easy mistake but I just can't find it...

    Just setting up an array to receive my input from TeraTerm and store it. It originally comes in the RXBUF one character at a time.

    When I run it, the compiler warns me that the array is declared but not used..


    Code (C):
    1. ISR(USCI_A0, serial_interrupt0)
    2. {
    3.   static int r;
    4.   char RX[5];
    5.  
    6.     switch(__even_in_range(UCA0IV,USCI_UART_UCTXCPTIFG))
    7.     {
    8.     case USCI_NONE:
    9.     break;
    10.     case USCI_UART_UCRXIFG:
    11.  
    12.     RX[r]= UCA0RXBUF; r++;
    13.     if(r==5) break;
    So when a character is received in the ISR, UCRXIFG is set and then the characters will be placed into the RX array one at a time by the variable 'r' incrementing the positions.
    However, it doesn't work and looks like it doesn't recognise RX[] as an array...

    Help would be appreciated,

    JSwale.
     
    Last edited: Sep 30, 2015
  2. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Could be because r has no assigned value.
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    Aside from r not being initialized (which is a problem that definitely needs to be fixed), you don't show enough code to determine if the array is ever used.

    Assigning a value to a variable and then never using that variable will be caught as a warning by many compilers. So do you every USE the values that get stored in your RX[] array?
     
  4. vpoko

    Member

    Jan 5, 2012
    258
    47
    I'm not sure if some compilers are different (or what the ANSI standard says), but for the C/C++ compilers I've used, static local variables (along with global variables) don't need to be initialized; they'll be set to 0 upon declaration. Non-static local variables do need to be initialized. If the variable isn't used, it might throw a warning, but it shouldn't throw an error. I actually can't see any problem in the posted code fragment (except I have no idea what type UCA0RXBUF is, is it 8 bits wide?)

    What's the exact error your compiler's throwing? Also, can you post the code to the end of the function?
     
    Last edited: Sep 30, 2015
  5. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    You are correct. Static and global variables are initialized to zero per the standard. There are C compilers that are not standards compliant and some of them have specifically NOT initialized static and global variables so that data can be saved and shared from one run to the next, which can be useful if the memory in question is nonvolatile such as Flash.

    It sounds like the compiler is throwing a warning about the array not being used (generally meaning that it's not being read -- writing to it doesn't count as use).
     
    vpoko likes this.
  6. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    Thanks for the replies.

    I have a little more information, the UCA0RXBUF is an unsigned short and receives the value and then I want to store it in the array. I don't want to use it yet, but will eventually use an IF statement to then set variables.....

    Code (C):
    1. if (RX[3]==72) x=10;
    When I debug... 'r' increments like it should AND the UCA0RXBUF is set to the value of the received char in decimal format BUT these values don't get placed into the array for some reason.

    The rest of the function is just other cases that have no effect on the original code.

    JSwale.
     
  7. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    IN the last line, you shouldn't use a conditional statement for the "break" In a case statement, a break should be unconditional.
     
  8. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    Depends entirely on what the logic of the following code is. From a style standpoint, I agree with you however. Then again, I'm not a proponent of every using break and continue statements in looping constructs, either.
     
  9. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    Is the RX[] array ever used in an expression anywhere within this function?
     
  10. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Break is used to correctly exit each case. It shoud not be used within a condition.
     
    Last edited: Oct 1, 2015
  11. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    The "break" statement is merely a control statement that performs an unconditional jump to the first line of code immediately following the smallest enclosing switch or iteration statement.

    It is used within conditional statements all the time where the conditional statement is there precisely for the purpose of deciding whether to break out of the switch or iteration statement or to proceed within it.
     
  12. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998

    All things in context. I've only refereces the switch statement case.

    Maybe in iteration, not in swtich statements. in almost 20 years, I've never seen code use it that way. Maybe some clever programmer got away with it, but that doens't mean it's the proper way to construct a switch statement.
     
    Last edited: Oct 1, 2015
  13. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    And notice that I specifically included the switch statement in my response. There is nothing syntactically or semantically wrong with putting a break statement in a conditional statement within a switch statement. Nothing at all. I might well argue that it is generally poor style, just as I would generally argue that goto statements should never be used unless there is a damn good reason. But damn good reasons DO exist and C is a language that is about performance, so it is not surprising that style-poor decisions are made all the time in the interest of performance.

    Consider the following (admittedly contrived) problem:

    Based on the character stored in variable ch, modify the value stored in x as described below:
    If the character is a 'C', multiply the value stored in x by 4.
    If the character is a 'B', multiply the value stored in x by 12.
    If the character is an 'A', if the value in x is more than 5, double it, otherwise multiply it by 24.

    There are many ways to implement this logic, but a perfectly valid one is

    Code (C):
    1.  
    2. switch (ch)
    3. {
    4.    case 'A': x *= 2.0;
    5.              if (x > 10.0)
    6.                 break;
    7.    case 'B': x *= 3.0;
    8.    case 'C': x *= 4.0;
    9. }
    10.  
    Now, this code is almost certainly a travesty from a performance standpoint, but that is because multiple multiplications are being done when a single one would suffice -- it would be trivial to construct another problem where that's not the case but similar logic applies. The point is that C allows the programmer great flexibility in deciding the flow-through behavior of the logic in a switch statement and programmers can and do use that flexibility for valid reasons to improve the performance of their code.
     
  14. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    As I said before, maybe someone is clever enough to make something done improperly work, but at the cost of (in this case) performance or (in other cases) unintended errors. I've not needed such construction in years of professional work, nor have a ever witnessed code written this way professionally. There is certainly much better and more proper ways to perform the stated function. Let the OP look at the line indicated and see if that's what he intended. It just might be the solution to his problem.
     
  15. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    It is not uncommon for even baseline C compilers to toss away variables that are never used. This may include those that get a value assigned to them, but never read to see that value.

    It's one simple optimization step, see if your compiler does this. Or just use the value somewhere to check if it comes back.
     
Loading...