473,750 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

undefined and unspecified behavior

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++] = i;

We say that the above expression statement produces undefined
behavior. Why can't we call it as unspecified behavior, because
nothing is said about the order of completion of side effects, which
affects the output?

Furthermore, I've found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.

Thanks in advance for the reply.

Mar 17 '07 #1
12 3064
On 17 Mar 2007 07:38:29 -0700, "Rajesh S R" <SR**********@g mail.com>
wrote:
>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++] = i;

We say that the above expression statement produces undefined
behavior. Why can't we call it as unspecified behavior, because
nothing is said about the order of completion of side effects, which
affects the output?

Furthermore, I've found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.

Thanks in advance for the reply.
Undefined behavior requires the use of a "nonportabl e or erroneous
program construct or of erroneous data" (paragraph 3.4.3). Your
example above meets this condition because while syntactically correct
it violates the restriction in paragraph 6.5-2 and is therefore
erroneous.

Unspecified behavior is not erroneous. It is a slightly more general
case of implementation-defined behavior. In both cases, the standard
provides two or more possibilities. For implementation-defined
behavior, the chosen possibility must be documented by every
(conforming) implementation. For unspecified behavior, there are no
further requirements so it need not be documented.
Remove del for email
Mar 17 '07 #2
Rajesh S R wrote:

<snip>
Furthermore, I've found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.
It's just a humorous way of mentioning that anything can happen after
undefined behaviour has been invoked in a program. I've heard that
that particular phrase was coined in clc by a former regular called
Kaz Kyzhelku. Apparently it's a common response when undefined
behaviour is invoked in C programs running on the mythical DS9000.

Mar 17 '07 #3
On Mar 17, 2:38 pm, "Rajesh S R" <SRRajesh1...@g mail.comwrote:

Unspecified behavior: The C Standard gives more than one possibility,
and it is not specified which of these possibilities is chosen, but
you know that one of them will be picked.

Undefined behavior: Anything can happen, and I mean absolutely
anything. Don't even think about trying to figure out what could
happen and what couldn't, because _anything_ can happen. In your
example, if the unspecified order in which side effects happen where
the only problem, then it would be "unspecifie d" behavior. But it is
worse: _Anything_ can happen, including formatting your hard disk,
your computer starting to send spam all over the place, or reporting
your credit card numbers to some hackers.

Mar 17 '07 #4
"Rajesh S R" <SR**********@g mail.comwrites:
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++] = i;

We say that the above expression statement produces undefined
behavior. Why can't we call it as unspecified behavior, because
nothing is said about the order of completion of side effects, which
affects the output?
In this case, you might assume that

a[i++] = i;

can only behave in one of a limited number of ways, simply because the
standard doesn't specify the order of evaluation; the "i" on the right
hand side of the assignment might be evaluated either before or after
"i" is incremented by the "++". (If that were the case, it would be
unspecified behavior.)

But in fact, the standard explicitly says that the behavior is
*undefined*, which means there are *no* constraints on the behavior.
It could quietly do exactly what you expect it to do, it could
increase i by 42 rather than by 1, it could clobber some unrelated
variable, it could crash the program, or it could crash your operating
system. Some of these outcomes are unlikely; the point is that the
standard doesn't preclude them.

The reason the standard allows this much freedom to the implementation
is basically to allow for optimizations. The compiler is allowed to
rearrange expressions to improve performance. To do so, it has to do
some compile-time analysis to make sure the rearrangement doesn't
break anything. The standard permits this analysis to *assume* that
no undefined behavior occurs. If that assumption is incorrect, it's
considered the programmer's fault that the code is broken.

Now in this particular case, the undefined behavior is something that
could fairly easily be detected at compile time. But pointers make
things more interesting. Rather than

a[i++] = i;

consider

a[(*p1)++] = (*p2)++;

The latter invokes undefined behavior only if p1 and p2 happen to
point to the same object, something the compiler can't necessarily
determine.
Furthermore, I've found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.
It's a humorous expression of the idea that the standard "places no
requirements" on the results of undefined behavior. Obviously a C
implementation can't literally make demons fly out of your nose -- but
if executing "a[i++] = i;" actually *did* make demons fly out of your
nose, that wouldn't tell you that the implementation is
non-conforming.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 17 '07 #5
Rajesh S R wrote:
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.
From N869:
3.19
[#1] unspecified behavior
behavior where this International Standard provides two or
more possibilities and imposes no requirements on which is
chosen in any instance

There are specified bounds on the results of unspecified behavior. It
might be an integer with some value.

Undefined behavior has no specified bounds, so may cause the computer to
crash, for example, which would not be permitted for unspecified behavior.
For example:
Consider the following code:
a[i++] = i;

We say that the above expression statement produces undefined
behavior. Why can't we call it as unspecified behavior, because
nothing is said about the order of completion of side effects, which
affects the output?
I can't give a good reason.
Furthermore, I've found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.
It is a fanciful image of something very unlikely. The point is that
since /that/ is permitted, any conceivable failure is permitted by the
standard (such as crashing, clobbering variables, restarting, you name
it). The shorthand phrase, then, covers all those and more!

--
Thad
Mar 17 '07 #6
santosh wrote:
Rajesh S R wrote:

<snip>
>Furthermore, I've found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.

It's just a humorous way of mentioning that anything can happen after
undefined behaviour has been invoked in a program. I've heard that
that particular phrase was coined in clc by a former regular called
Kaz Kyzhelku. Apparently it's a common response when undefined
behaviour is invoked in C programs running on the mythical DS9000.
What mythical? Go here:

http://dialspace.dial.pipex.com/town/green/gfd34/art/

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 17 '07 #7
santosh wrote:
Rajesh S R wrote:

<snip>
Furthermore, I've found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.

It's just a humorous way of mentioning that anything can happen after
undefined behaviour has been invoked in a program. I've heard that
that particular phrase was coined in clc by a former regular called
Kaz Kyzhelku. Apparently it's a common response when undefined
behaviour is invoked in C programs running on the mythical DS9000.

I've usually seen a fellow name John Woods (who I don't recall
personally) credited with coining it. This may be it:

<http://groups.google.c om/group/comp.std.c/msg/dfe1ef367547684 b>

Kaz famously had said something like, "I ran it on the Deathstation
9000, and demons flew out of my nose." That became a .sig for one or
more regulars over the years.

I can't find Kaz's original post (hey, it's Saturday) only follow-ups.

<http://groups.google.c om/group/comp.arch/msg/3095a0d47c235c8 6>
The more you dig, the more you realize how many messages aren't in the
archives.

Brian

Mar 17 '07 #8
Default User wrote:
>
.... snip ...
>
The more you dig, the more you realize how many messages aren't
in the archives.
Assuming you mean Google by archives, that may be because of
anti-social x-noarchive headers, or because the originator
specifically asked that it be removed. The latter is a perfectly
respectable means of retracting foolishness.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/ (taming google)
<http://members.fortune city.com/nnqweb/ (newusers)

--
Posted via a free Usenet account from http://www.teranews.com

Mar 18 '07 #9
CBFalconer wrote:
Default User wrote:
... snip ...

The more you dig, the more you realize how many messages aren't
in the archives.

Assuming you mean Google by archives,
Yes. I know there are other archives, but I don't of any that are
publicly searchable.
that may be because of
anti-social x-noarchive headers, or because the originator
specifically asked that it be removed.
Kaz has many posts in Google, including many with that email address. I
don't know much about Mr. Woods.

There are posts of mine that are missing, that neither of the above
apply to.
The latter is a perfectly
respectable means of retracting foolishness.
Neither post that I found referenced seems to fit that category, but
you never know.

Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Mar 18 '07 #10

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

Similar topics

24
1611
by: DaKoadMunky | last post by:
I was recently reading an article about sequence points that used the canonical i = i++; as an illustration of modifying a variable multiple times between sequence points. Curiously the article did not indicate the type of i. Is it fair to say that if i was an instance of a class type that overloaded
15
2579
by: shablool | last post by:
Hi all, Two distinct compilers give different output for the following code: #include <iostream> #include <string> int main(void) { std::string s("0124"); s.replace(0, 3, s).replace(s.size(), 6, s);
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 ??
12
1806
by: RoSsIaCrIiLoIA | last post by:
On Mon, 07 Feb 2005 21:28:30 GMT, Keith Thompson <kst-u@mib.org> wrote: >"Romeo Colacitti" <wwromeo@gmail.com> writes: >> Chris Torek wrote: >>> In article <4205BD5C.6DC8@mindspring.com> >>> pete <pfiland@mindspring.com> wrote: > >>> >If you have >>> > int array; >>> >then
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; }
14
2572
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
5
13690
by: fjanon | last post by:
Hi, I went through the HTML spec without finding the description of what the browsers behavior should/must be regarding tags or attributes they don't understand. I did a quick experiment like this: <mytag style='display:none'> <h3>Hello</h3>
26
2192
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work perfectly.)
8
2047
by: Aftabpasha | last post by:
In a function call like printf("%d,..",++i,++i,..) can we consider every ++i as a Full Expression? And if it is a full expression, it means there is a sequence point after every ++i. So, uncertainty exists only about order of evaluation of arguments and not about the side-effects of each of the ++i expressions causing Undefined behavior. I just want to know whether such function calls cause Undefined Behavior or just Unspecified Behavior. If...
0
9577
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
9396
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
9339
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...
0
6081
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
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
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
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
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.