Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > bogus warning?

Reply
Thread Tools

bogus warning?

 
 
steve
Guest
Posts: n/a
 
      10-13-2011
When compiling the program

int main(void)
{
short a;

a = a + a;

return 0;
}

with gcc 4.3.3 -Wconversion I get the warning:

warning: conversion to 'short int' from 'int' may alter its value.

It looks like 'a' got promoted to an int. But I thought operands only
got promoted to the lowest type that included all the operands. FWIW,
I got the same warning when I changed the line to a=a+1. But I did
NOT get the warning when I just had a++.

Or is this warning completely bogus?

--
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      10-13-2011
steve <> writes:
>warning: conversion to 'short int' from 'int' may alter its value.


Read the prepositions carefully: /to/ 'short int' /from/ int.

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      10-13-2011
On 10/13/2011 04:08 PM, steve wrote:
> When compiling the program
>
> int main(void)
> {
> short a;
>
> a = a + a;
>
> return 0;
> }
>
> with gcc 4.3.3 -Wconversion I get the warning:
>
> warning: conversion to 'short int' from 'int' may alter its value.
>
> It looks like 'a' got promoted to an int. But I thought operands only
> got promoted to the lowest type that included all the operands.


For "An object or expression with an integer type whose integer
conversion rank is less than or equal to the rank of int and unsigned
int" or "A bit-field of type _Bool, int, signed int, or unsigned int",
"If an int can represent all values of the original type, the value is
converted to an int; otherwise, it is converted to an unsigned int.
These are called the integer promotions.4 All other types are
unchanged by the integer promotions." (6.3.1.1p2).

> FWIW,
> I got the same warning when I changed the line to a=a+1. But I did
> NOT get the warning when I just had a++.
>
> Or is this warning completely bogus?


Your code also involves a conversion of 'int' back to 'short int', and
that's what the waring is about. That conversion can cause problems if,
before the sum, a > SHRT_MAX/2 || a < SHRT_MIN/2.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-13-2011
steve <> writes:

> When compiling the program
>
> int main(void)
> {
> short a;
>
> a = a + a;
>
> return 0;
> }
>
> with gcc 4.3.3 -Wconversion I get the warning:
>
> warning: conversion to 'short int' from 'int' may alter its value.
>
> It looks like 'a' got promoted to an int. But I thought operands only
> got promoted to the lowest type that included all the operands.


Yes, 'a' gets promoted to int. Integer types shorter than int get
promoted to int (or unsigned int) rather than to the lowest ranked type
that includes both operands.

The gory details can be found in the C standard.

> FWIW,
> I got the same warning when I changed the line to a=a+1. But I did
> NOT get the warning when I just had a++.
>
> Or is this warning completely bogus?


Not at all. It's warning you that the conversion implied by the
assignment (do you see that the warning is about the conversion from int
back to short int?) might go wrong. If, for example, 'a' is more that
SHRT_MAX/2 then 'a + a' may be perfectly well defined, but the
conversion back to short int could go wrong.

--
Ben.
 
Reply With Quote
 
steve
Guest
Posts: n/a
 
      10-13-2011
On Oct 13, 4:08*pm, James Kuyper <jameskuy...@verizon.net> wrote:

> "If an int can represent all values of the original type, the value is
> converted to an int; otherwise, it is converted to an unsigned int.
> These are called the integer promotions.4 All other types are
> unchanged by the integer promotions." (6.3.1.1p2).


That's what I needed to know but couldn't find. Thanks!

--

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-13-2011
(Stefan Ram) writes:

> steve <> writes:
>>warning: conversion to 'short int' from 'int' may alter its value.

>
> Read the prepositions carefully: /to/ 'short int' /from/ int.


Why do you think the OP misread them? I take your point that he might
have missed that (and in my reply I added a remark to that effect after
having seen your post) but his question makes more sense assuming that
he did not.

--
Ben.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      10-13-2011
Ben Bacarisse <> writes:
>Why do you think the OP misread them?


The preposition order in English is »from ... to ...«,
for example:

»I have gone from rags to riches.«,

not

*»I have gone to riches from rags.«.

 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      10-13-2011
(Stefan Ram) writes:
> Ben Bacarisse <> writes:
> >Why do you think the OP misread them?

>
> The preposition order in English is »from ... to ...«,
> for example:
>
> »I have gone from rags to riches.«,
>
> not
>
> *»I have gone to riches from rags.«.


http://www.google.com/search?q=%22get+to+*+from%22
= 3 billion webpages that disagree

Modern internet usage aside, it's perfectly natural English;
you'll even find to...from in Shakespeare, I'm sure.

Phil
--
Unix is simple. It just takes a genius to understand its simplicity
-- Dennis Ritchie (1941-2011), Unix Co-Creator,
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-14-2011
(Stefan Ram) writes:

> Ben Bacarisse <> writes:
>>Why do you think the OP misread them?

>
> The preposition order in English is »from ... to ...«,
> for example:
>
> »I have gone from rags to riches.«,
>
> not
>
> *»I have gone to riches from rags.«.


What was your suggestion that the OP "read the prepositions carefully"
for? To prompt the OP to submit a bug report about gcc's grammar?

--
Ben.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      10-14-2011
Ben Bacarisse <> writes:
>What was your suggestion that the OP "read the prepositions carefully"
>for?


A warning about converting from int to short would be
natural, since a narrowing conversion obviously might
destroy information.

A warning about a widening conversion from short to int
would be strange. Since no information can be lost, there
is no need for a warning in this case.

Since the OP was wondering about the warning, he must have
read the warning as a warning about a widening conversion.
(There would be no reason to wonder about a warning about
a narrowing conversion, since a warning would be normal
in this case.)

Since the warning was about a narrowing conversion, however,
he must have misread the warning about a narrowing
conversion as a warning about a widening conversion.

Thus, he must have mistakenly read the »form« as a »to« and
the »to« as a »from«.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NTP on a router picking up a bogus server John Caruso Cisco 5 11-26-2005 04:58 PM
bogus questions on PrepLogic Tina MCSD 3 10-11-2004 11:24 PM
Filtering bogus TCP packets David Cisco 5 06-03-2004 12:45 PM
firewalls "removing bogus header" Karl Seguin ASP .Net 4 02-10-2004 06:31 PM
This is such a bogus article George Hester ASP .Net 1 07-31-2003 12:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57