Search code examples
c++gccbuildgcc-warningmitk

Error: this statement may fall through [-Werror=implicit-fallthrough=]


I am trying to compile mitk on ubuntu and I got this error :

error: this statement may fall through [-Werror=implicit-fallthrough=]

Here there is a part of code :

      /** Get memory offset for a given image index */
      unsigned int GetOffset(const IndexType & idx) const
      {
       const unsigned int * imageDims = m_ImageDataItem->m_Dimensions;

        unsigned int offset = 0;
        switch(VDimension)
        {
        case 4:
         offset = offset + idx[3]*imageDims[0]*imageDims[1]*imageDims[2];
        case 3:
        offset = offset + idx[2]*imageDims[0]*imageDims[1];
        case 2:
        offset  = offset + idx[0] + idx[1]*imageDims[0];
         break;
        }

        return offset;
      }

I would be appreciate for any help please.


Solution

  • You should add keyword break to each case statement, if you don't do that, the code will run from case which it matches condition and continue to meet the

    break;

    for example: If VDimension = 4, then the code will run from case 4 => continue to case 3 => contine to case 2 then break. It means that it will execute below commands:

    offset = offset + idx[3]*imageDims[0]*imageDims[1]*imageDims[2];
    offset = offset + idx[2]*imageDims[0]*imageDims[1];
    offset  = offset + idx[0] + idx[1]*imageDims[0];
    break;
    return offset;
    

    I think your code should be:

    /** Get memory offset for a given image index */
      unsigned int GetOffset(const IndexType & idx) const
      {
       const unsigned int * imageDims = m_ImageDataItem->m_Dimensions;
    
        unsigned int offset = 0;
        switch(VDimension)
        {
        case 4:
         offset = offset + idx[3]*imageDims[0]*imageDims[1]*imageDims[2];
         break;
        case 3:
         offset = offset + idx[2]*imageDims[0]*imageDims[1];
         break;
        case 2:
         offset  = offset + idx[0] + idx[1]*imageDims[0];
         break;
        }
    
        return offset;
      }