bogus warning?

Discussion in 'C Programming' started by steve, Oct 13, 2011.

  1. steve

    steve Guest

    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?

    --
    steve, Oct 13, 2011
    #1
    1. Advertising

  2. steve

    Stefan Ram Guest

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


    Read the prepositions carefully: /to/ 'short int' /from/ int.
    Stefan Ram, Oct 13, 2011
    #2
    1. Advertising

  3. steve

    James Kuyper Guest

    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.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.
    James Kuyper, Oct 13, 2011
    #3
  4. 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.
    Ben Bacarisse, Oct 13, 2011
    #4
  5. steve

    steve Guest

    On Oct 13, 4:08 pm, James Kuyper <> 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.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!

    --
    steve, Oct 13, 2011
    #5
  6. -berlin.de (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.
    Ben Bacarisse, Oct 13, 2011
    #6
  7. steve

    Stefan Ram Guest

    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.«.
    Stefan Ram, Oct 13, 2011
    #7
  8. steve

    Phil Carmody Guest

    -berlin.de (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="get to * from"
    = 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,
    Phil Carmody, Oct 13, 2011
    #8
  9. -berlin.de (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.
    Ben Bacarisse, Oct 14, 2011
    #9
  10. steve

    Stefan Ram Guest

    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«.
    Stefan Ram, Oct 14, 2011
    #10
  11. steve

    Ian Collins Guest

    On 10/14/11 03:30 PM, Stefan Ram wrote:
    > 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«.


    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.

    --
    Ian Collins
    Ian Collins, Oct 14, 2011
    #11
  12. steve

    James Kuyper Guest

    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.
    --
    James Kuyper
    James Kuyper, Oct 14, 2011
    #12
  13. steve

    steve Guest

    On Oct 13, 10:16 pm, James Kuyper <> wrote:
    > 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.
    > --
    > James Kuyper


    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.
    steve, Oct 14, 2011
    #13
  14. steve

    Stefan Ram Guest

    Ian Collins <> writes:
    >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.
    Stefan Ram, Oct 14, 2011
    #14
  15. -berlin.de (Stefan Ram) writes:
    > Ian Collins <> writes:
    >>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.


    It happens to all of us.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 14, 2011
    #15
  16. "christian.bau" <> writes:

    > On Oct 13, 10:16 pm, Ben Bacarisse <> wrote:
    >
    >> 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.

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

    --
    Ben.
    Ben Bacarisse, Oct 15, 2011
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Seguin

    firewalls "removing bogus header"

    Karl Seguin, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,385
    George Ter-Saakov
    Feb 10, 2004
  2. Doug

    I.E. Bogus Progress Meter?

    Doug, Aug 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    339
    Kevin Spencer
    Aug 3, 2004
  3. Tina

    PrepLogic bogus Answers

    Tina, Oct 11, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    359
    clintonG
    Oct 12, 2004
  4. Alan Silver
    Replies:
    1
    Views:
    326
    Alan Silver
    Feb 23, 2006
  5. Replies:
    0
    Views:
    274
Loading...

Share This Page