473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

undefined behavior?

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
Nov 13 '05 #1
5 2010
"Mantorok Redgormor" <ne*****@tokyo.com> schrieb im Newsbeitrag
news:41**************************@posting.google.c om...> 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
Nov 13 '05 #2
In article <41**************************@posting.google.com >,
ne*****@tokyo.com (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.
Nov 13 '05 #3
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 : bi****@eton.powernet.co.uk
"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
Nov 13 '05 #4
On Thu, 16 Oct 2003 00:12:06 +0100, Christian Bau
<ch***********@cbau.freeserve.co.uk> wrote in comp.lang.c:
In article <41**************************@posting.google.com >,
ne*****@tokyo.com (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
Nov 13 '05 #5
Jack Klein <ja*******@spamcop.net> wrote:
On Thu, 16 Oct 2003 00:12:06 +0100, Christian Bau
<ch***********@cbau.freeserve.co.uk> wrote in comp.lang.c:
In article <41**************************@posting.google.com >,
ne*****@tokyo.com (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
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: marbac | last post by:
Hi, i heard a lot about "undefined behaviour" in this and other newsgroups dealing with c/c++. Is there a list where all cases with undefined behaviour in C++ are listed? regards marbac
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
66
by: Mantorok Redgormor | last post by:
#include <stdio.h> struct foo { int example; struct bar *ptr; }; int main(void) { struct foo baz; baz.ptr = NULL; /* Undefined behavior? */ return 0;
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
30
by: jimjim | last post by:
Hello, #include <stdio.h> int main(int argc, char *argv) { int x = 1; printf("%d %d %d\n", ++x, x, x++); return 0; }
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
14
by: avsharath | last post by:
In "Bjarne Stroustrup's C++ Style and Technique FAQ" at: http://www.research.att.com/~bs/bs_faq2.html#evaluation-order for the statement: f(v,i++); he says that "the result is undefined...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
22
by: blargg | last post by:
Does ~0 yield undefined behavior? C++03 section 5 paragraph 5 seems to suggest so: The description of unary ~ (C++03 section 5.3.1 paragraph 8): But perhaps "one's complement" means the...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.