This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

unknown warning

u8 Key;
u8 VarItem;

      switch (Key) {
        case Kb_0    : ...
        case Kb_1    : ...
        case Kb_2    : ...
        case Kb_3    : ...
        case Kb_4    : ...
        case Kb_5    : ...
        case Kb_6    : ...
        case Kb_7    : ...
        case Kb_8    : ...
        case Kb_9    : ...
        case Kb_Min  : ...
        case Kb_Enter :
            switch (VarItem) {
              case 0  : ...
              case 1  : ...
              case 2  : ...
             }
      }


When I compile above I got one warning
warning: #516-D: transfer of control bypasses initialisation off:
in the line near switch(VarItem). What is the meaning of this warning. When I ignore it, all works, but I do not like warnings ...

Parents Reply Children
  • You showed too little code.

    One of your case alternatives assigns a value to one of your variables, and the compiler can not know if this is a state machine that is guaranteed to pass this specific state.

  • Thanks for response.

    Problem solved ! but I do not see any reason to generate a warning.
    The source code is a C++ file !

    Following code gives a warning.

    void Test(u8 num) {
      switch (num) {
        case 1 : long test = 12;
                    break;
        case 2 :
                    break;
      }
    }
    


    following code does not.

    void Test(u8 num) {
     long test;
      switch (num) {
        case 1 : test = 12;
                    break;
        case 2 :
                    break;
      }
    }
    


    It seems that when you declare a variable in a switch, the compiler generates a warning.
    I do not see why, but however, the problem is solved ...

    Luc Vercruysse

  • "I do not see any reason to generate a warning. The source code is a C++ file!"

    Lots of C++ source files generate warnings!
    It's up to you to fix your source code!

    void Test(u8 num) {
      switch (num) {
        case 1 : long test = 12;
                    break;
        case 2 :
                    break;
      }
    }
    


    A warning seems absolutely in order here!
    in case 1, you both define and initialise 'test', but in case 2, you skip both the definition and the initialisation - what do you expect the compiler to make of this?!

    The compiler warnings describes exactly what you are doing:
    "transfer of control" = the jump to case 2;
    "bypasses initialisation" - well, obviously the initialisation at case 1 is bypassed when the execution jumps straight to case 2, isn't it?!

  • "... I do not see why ..."

    A new definition can occur within a brace-enclosed block:

    void Test(u8 num) {
      switch (num) {
        case 1: {
          long test = 12;
          break;
        }
        case 2:
          break;
      }
    }