473,700 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2031
"Mantorok Redgormor" <ne*****@tokyo. com> schrieb im Newsbeitrag
news:41******** *************** ***@posting.goo gle.com...> 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.c om (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 "implementa tion 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.pow ernet.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.c om (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 "implementa tion 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 "implementa tion-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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #5
Jack Klein <ja*******@spam cop.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.c om (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 "implementa tion 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 "implementa tion-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
3073
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
2574
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 happen" if your C code invokes "undefined behavior". Behavior not defined by the ANSI/ISO C 9 standard may be defined by some other standard (i.e. POSIX) or it may be defined by your compiler, your operating system or your machine architecture.
66
3049
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
3090
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 don't have a copy of ANSI C89 standard,therefore i had to post this question: What is the difference between "unspecified" behaviour & "undefined" behaviour of some C Code ??
30
22741
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
2328
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
2566
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 because the order of evaluation of function arguments are undefined". But AFAIK, the order of evaluation of function arguments is unspecified
12
3061
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 difference. For example: Consider the following code: a = i; We say that the above expression statement produces undefined
22
2016
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 value that type would have with all bits inverted, rather than the mathematical result of inverting all bits in the binary representation. For example, on a machine with 32-bit
33
2827
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. I am having some confusion with the former statement! Also, state the reason for the statement being undefined!
0
9202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8909
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6555
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5894
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4395
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4649
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3081
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2371
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.