[previous] 2945 [next] Constant: Result of cast is only representable in a two's complement implementation.
Conversion to signed

A negative value is being cast to a signed type and the value is such that it can only be represented in a signed type of this size in a two's-complement form. In practice, two's complement representation is almost universal in modern computers, but other binary representations do exist.

Message 2945 will be generated when:

Notice that on a 16 bit architecture:
  1. 32767 is a constant of type signed int
  2. 32768 is a constant of type signed long
  3. -32768 is an expression of type signed long

The expression "-32767 - 1" is of type signed int - BUT its evaluation will result in overflow if the signed representation is not two's complement.

Example:


/*PRQA S 274,280,1278,2017,2982,2984,3120,3198,3210,3212,3408,3447,3602,3606 ++*/
/***************************************************/
/* OPTIONS: -s int=16                              */
/***************************************************/

extern signed char  sca;
extern signed short ssa;
extern signed int   sia;

extern void foo(void)
{
    sca = (signed char)(-127);
    sca = (signed char)(-128);                         /* 2945 */
    sca = (signed char)(-129);                         /* 2855 */

    ssa = (signed short)(-32767);                      /*      */
    ssa = (signed short)(-32767 - 1);                  /* 2945 */
    ssa = (signed short)(-32769L);                     /* 2855 */

    sia = (signed int)(-32767);                        /*      */
    sia = (signed int)(-32767 - 1);                    /* 2945 */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents