understanding a warning in RTEMS

Nicholas Sherlock n.sherlock@gmail.com
Sun May 10 07:42:00 GMT 2009


Chris Johns wrote:
> objwarning.c:26: warning: conversion to 'uint32_t' from 'int' may change 
> the sign of the result
> 
> 
> typedef uint32_t   Objects_Id;
> typedef uint16_t   Objects_Maximum;
> 
> RTEMS_INLINE_ROUTINE uint16_t _Objects_Get_index(
>   Objects_Id id)
> 
> uint32_t foo(Objects_Id id, Objects_Id minimum_id, uint16_t v)
> {
>   uint32_t    block;
>   uint32_t    block_2;
>   block = _Objects_Get_index( id ) - _Objects_Get_index( minimum_id );
>   block_2 = _Objects_Get_index_2( id ) - _Objects_Get_index_2( 
> minimum_id );
>   return block + block_2;
> }

I think the problem is that, although the return type of 
_Objects_Get_Index is uint16_t, you're subtracting another value in your 
assignment expressions on block and block_2. The result of the 
subtraction is a signed int. You need to explicitly cast it to uint32_t 
when you assign to block and block_2 - you're telling the compiler that, 
although it is performing a subtraction, the result won't be negative, 
and so it'll fit in the unsigned type.

Cheers,
Nicholas Sherlock



More information about the Gcc-help mailing list