From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CVI 2013 and switch statement default case

a

Review this code.   CVI 2013 gives me   "warning: will never be executed" at line XXX.

typedef enum bandType { Band900, Band1800, Band1900 };

int main (int argc, char *argv[])
{
  enum bandType band = Band900;
 
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1;    /* out of memory */
 
 band = 512;   //  Line A,  not a normal use of enum but does execute
               // and sets the variable to 512
 
 switch (band) // Switch statement with an enum type      
 {
  case Band900:
    // Do something
   break;
  case Band1800:
    // Do something
   break;
  case Band1900:
    // Do something
   break;
  default:
    // Line XXX.  Execution can get here, despite warning that it can not
   break;
 }
 
 I think the warning is wrong.
 
 My team uses the default case to throw an error for two reasons.  One is to catch if an enum is out of range.  Two is for future growth.  We can expand the definition of the enum (for example add Band2100).  If a section of code is not updated for the expanded enum, than the default case throws an error message.
 
 I think I have seen a similiar issue with an if statement, though I am not completely confident.  Example:
 if( str(x) < 100)
  // Do something,  but I got the warning here
  
 Your thoughts please

0 Kudos
Message 1 of 4
(5,551 Views)

When investigating your case in the interactive execution window I came across the following behavior:

 

enum zahl { EINS, ZWEI, DREI, VIER };

run in the interactive execution window gives two errors about invalid allocation size...

0 Kudos
Message 2 of 4
(5,545 Views)

I think that "band = 512;" is undefined behaviour not covered by the C standards. A perfect compiler could warn there. But no compiler is perfect and a warning isn't required by the standards.

The warning for the case statement is ok in my opinion because the default case depends on the undefined behaviour before.

0 Kudos
Message 3 of 4
(5,526 Views)

Hi Wofgang,

 

Can you give additional details on these two allocation size errors?  If you could provide an exact snippet to recreate them, that would be most helpful.

 

I am not seeing these errors in the Interactive Execution window when I make a similar declaration.

 

Thanks,

Daniel

 

Edit:  I found your follow up post here: http://forums.ni.com/t5/LabWindows-CVI/Interactive-Execution-fails-miserably/td-p/2613011 .  It appears this issue will more than likely be included as a fix in our next release.  

Daniel Dorroh
National Instruments
0 Kudos
Message 4 of 4
(5,448 Views)