473,750 Members | 2,292 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 2034
"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
3082
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
2579
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
3058
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
3096
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
22748
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
2334
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
2571
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
3064
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
2021
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
2842
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
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
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
9394
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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
6803
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
6080
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
4712
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
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.