undefined behavior?

Discussion in 'C Programming' started by Mantorok Redgormor, Oct 15, 2003.

  1. If I do something like the following:

    unsigned int bar=10;

    Then use, -bar with the unary '-' and assign this value to an int,
    does this invoke undefined behavior in anyway?

    Basically I have:
    unsigned int bar=10;
    int foo;
    foo = -bar; /* Undefined behavior? */
    I'm just wondering if this value that is expressed in the expression
    -bar can be representable at all times with the int variable and in no
    way ends up invoking undefined behavior
    Mantorok Redgormor, Oct 15, 2003
    #1
    1. Advertising

  2. Mantorok Redgormor

    cody Guest

    "Mantorok Redgormor" <> schrieb im Newsbeitrag
    news:...> If I do something
    like the following:
    >
    > unsigned int bar=10;
    >
    > Then use, -bar with the unary '-' and assign this value to an int,
    > does this invoke undefined behavior in anyway?
    >
    > Basically I have:
    > unsigned int bar=10;
    > int foo;
    > foo = -bar; /* Undefined behavior? */
    > I'm just wondering if this value that is expressed in the expression
    > -bar can be representable at all times with the int variable and in no
    > way ends up invoking undefined behavior



    " 6.3.1.3 Signed and unsigned integers

    [#1] When a value with integer type is converted to another |
    integer type other than _Bool, if the value can be
    represented by the new type, it is unchanged.

    [#2] Otherwise, if the new type is unsigned, the value is
    converted by repeatedly adding or subtracting one more than
    the maximum value that can be represented in the new type
    until the value is in the range of the new type.

    [#3] Otherwise, the new type is signed and the value cannot
    be represented in it; the result is implementation-defined."


    Well, not undefined, but implementation defined.

    --
    cody

    [Freeware, Games and Humor]
    www.deutronium.de.vu || www.deutronium.tk
    cody, Oct 16, 2003
    #2
    1. Advertising

  3. In article <>,
    (Mantorok Redgormor) wrote:

    > If I do something like the following:
    >
    > unsigned int bar=10;
    >
    > Then use, -bar with the unary '-' and assign this value to an int,
    > does this invoke undefined behavior in anyway?
    >
    > Basically I have:
    > unsigned int bar=10;
    > int foo;
    > foo = -bar; /* Undefined behavior? */
    > I'm just wondering if this value that is expressed in the expression
    > -bar can be representable at all times with the int variable and in no
    > way ends up invoking undefined behavior


    First, "-bar" will give a very large positive number, and not a negative
    number. If unsigned int = 32 bit then -bar equals 2^32 - 10.

    Second, if that number is too large to fit into an unsigned int, then
    the behavior is "implementation defined". So your compiler should really
    document what will happen. It is defined behaviour, so the program will
    not crash.
    Christian Bau, Oct 16, 2003
    #3
  4. Mantorok Redgormor wrote:

    > If I do something like the following:
    >
    > unsigned int bar=10;
    >
    > Then use, -bar with the unary '-' and assign this value to an int,
    > does this invoke undefined behavior in anyway?
    >
    > Basically I have:
    > unsigned int bar=10;
    > int foo;
    > foo = -bar; /* Undefined behavior? */
    > I'm just wondering if this value that is expressed in the expression
    > -bar can be representable at all times with the int variable and in no
    > way ends up invoking undefined behavior


    Consider this code:

    unsigned int bar = UINT_MAX;
    int foo = -bar;

    The value is unrepresentable as a signed int, so the result is
    implementation-defined (see 3.2.1 of the ANSI C Standard).

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Oct 16, 2003
    #4
  5. Mantorok Redgormor

    Jack Klein Guest

    On Thu, 16 Oct 2003 00:12:06 +0100, Christian Bau
    <> wrote in comp.lang.c:

    > In article <>,
    > (Mantorok Redgormor) wrote:
    >
    > > If I do something like the following:
    > >
    > > unsigned int bar=10;
    > >
    > > Then use, -bar with the unary '-' and assign this value to an int,
    > > does this invoke undefined behavior in anyway?
    > >
    > > Basically I have:
    > > unsigned int bar=10;
    > > int foo;
    > > foo = -bar; /* Undefined behavior? */
    > > I'm just wondering if this value that is expressed in the expression
    > > -bar can be representable at all times with the int variable and in no
    > > way ends up invoking undefined behavior

    >
    > First, "-bar" will give a very large positive number, and not a negative
    > number. If unsigned int = 32 bit then -bar equals 2^32 - 10.
    >
    > Second, if that number is too large to fit into an unsigned int, then
    > the behavior is "implementation defined". So your compiler should really
    > document what will happen. It is defined behaviour, so the program will
    > not crash.


    Not a chance, assigning an integer expression, literal, or value to
    any unsigned integer type is never "implementation-defined".

    The value is exactly defined, although the resulting value is
    implementation-defined, because it depends on the maximum value of the
    unsigned destination type is implementation-defined.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Oct 16, 2003
    #5
  6. Mantorok Redgormor

    Richard Bos Guest

    Jack Klein <> wrote:

    > On Thu, 16 Oct 2003 00:12:06 +0100, Christian Bau
    > <> wrote in comp.lang.c:
    >
    > > In article <>,
    > > (Mantorok Redgormor) wrote:
    > >
    > > > If I do something like the following:
    > > >
    > > > unsigned int bar=10;
    > > >
    > > > Then use, -bar with the unary '-' and assign this value to an int,
    > > > does this invoke undefined behavior in anyway?
    > > >
    > > > Basically I have:
    > > > unsigned int bar=10;
    > > > int foo;
    > > > foo = -bar; /* Undefined behavior? */
    > > > I'm just wondering if this value that is expressed in the expression
    > > > -bar can be representable at all times with the int variable and in no
    > > > way ends up invoking undefined behavior

    > >
    > > First, "-bar" will give a very large positive number, and not a negative
    > > number. If unsigned int = 32 bit then -bar equals 2^32 - 10.
    > >
    > > Second, if that number is too large to fit into an unsigned int, then
    > > the behavior is "implementation defined". So your compiler should really
    > > document what will happen. It is defined behaviour, so the program will
    > > not crash.

    >
    > Not a chance, assigning an integer expression, literal, or value to
    > any unsigned integer type is never "implementation-defined".


    But foo is a _signed_ integer. It is not the validity of -bar that is
    questioned, but the validity of assigning -bar to a signed int.

    Richard
    Richard Bos, Oct 16, 2003
    #6
    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. aka
    Replies:
    10
    Views:
    690
  2. REH
    Replies:
    25
    Views:
    819
    Victor Bazarov
    Mar 29, 2005
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,724
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    577
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    357
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page