undefined behavior?

M

Mantorok Redgormor

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
 
C

cody

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

Christian Bau

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

Richard Heathfield

Mantorok said:
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).
 
J

Jack Klein

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
 
R

Richard Bos

Jack Klein said:
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
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top