bogus warning?

S

steve

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?

--
 
J

James Kuyper

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.48) 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.
 
B

Ben Bacarisse

steve said:
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.
 
S

steve

"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.48) 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!

--
 
B

Ben Bacarisse

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.
 
S

Stefan Ram

Ben Bacarisse said:
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.«.
 
B

Ben Bacarisse

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?
 
S

Stefan Ram

Ben Bacarisse said:
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«.
 
I

Ian Collins

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«.

That's a pretty big stretch when the OP wrote:

"But I thought operands only got promoted to the lowest type that
included all the operands."

That looks to me like he wasn't expecting any promotions. A perfectly
logical assumption.
 
J

James Kuyper

On 10/13/2011 10:30 PM, Stefan Ram wrote:
....
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.)

As I read his message, he was unaware that any conversion at all was
taking place, and would therefore have been equally surprised by any
message about any conversion, whether widening or shortening.
 
S

steve

On 10/13/2011 10:30 PM, Stefan Ram wrote:
...


As I read his message, he was unaware that any conversion at all was
taking place, and would therefore have been equally surprised by any
message about any conversion, whether widening or shortening.

You are correct sir. I was surprised that there was any conversion at
all since both operands were 'short'. Never the less, apparently I am
getting senile since a search of the c.l.c archives show that I asked
a similar question question several years ago.
 
S

Stefan Ram

Ian Collins said:
That looks to me like he wasn't expecting any promotions. A perfectly
logical assumption.

From steve's first reply I already learned that I erred with
my assumptions about steve's knowledge. Moreover, in the
meantime, I also have learned that I was wrong with my ideas
about the from-to-phrase-order. So, I was totally wrong by
all means.
 
K

Keith Thompson

From steve's first reply I already learned that I erred with
my assumptions about steve's knowledge. Moreover, in the
meantime, I also have learned that I was wrong with my ideas
about the from-to-phrase-order. So, I was totally wrong by
all means.

It happens to all of us.
 
B

Ben Bacarisse

christian.bau said:
If short = 16 bits, and int = 16 bits, then the situation would be
exactly the same, that is you will get a correct result for exactly
the same values, and an incorrect result for exactly the same values.
However, the conversion from int to short couldn't possibly fail. And
the addition that fails would be an addition of ints. So should the
compiler give a warning in that case?

The conversion can't go wrong so, no, there should be no warning.

In the situation you describe, it would be the undefined behaviour on
overflow that might be warned about; not the now safe conversion from
int to short. I doubt anyone would advocate warning about potential
overflow since there would be far too many "false positives".

Conversions from longer to shorter types are more rare, so a warning
about it might be considered helpful. Even so it's worth pointing out,
I think, that the warning in question has to be asked for. It is not
even included in -Wall or -Wextra.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top