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

Home Posts Topics Members FAQ

f(v[i],i++); is Undefined or Unspecified?

In "Bjarne Stroustrup's C++ Style and Technique FAQ" at:
http://www.research.att.com/~bs/bs_f...aluation-order

for the statement:
f(v[i],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
as the C++ Standard says:
"5.2.2 Function
The order of evaluation of arguments is unspecified."

So isn't he wrong at this?

I think it is undefined because the value of 'i' is accessed and also
moidified within the sequence point and hence it is undefined.

Sharath A.V

Jul 16 '06 #1
14 2571
av*******@gmail .com wrote:
In "Bjarne Stroustrup's C++ Style and Technique FAQ" at:
http://www.research.att.com/~bs/bs_f...aluation-order

for the statement:
f(v[i],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
as the C++ Standard says:
"5.2.2 Function
The order of evaluation of arguments is unspecified."

So isn't he wrong at this?

I think it is undefined because the value of 'i' is accessed and also
moidified within the sequence point and hence it is undefined.

Sharath A.V
According to the standard the value of f is unspecified but what does
that mean in practice? Undefined results because we cannot guarantee
what the result of calling f will be. I think we can allow the inventor
of the language a little leeway in describing the effects of an error.
Besides if every FAQ or textbook used the language of the standard
without explanation they would be virtually unreadable.

Cheers
Jim.
Jul 16 '06 #2
James Bannon wrote:
>>
According to the standard the value of f is unspecified but what does
that mean in practice? Undefined results because we cannot guarantee
what the result of calling f will be.
Well, in a way, yes, but this is sloppy wording. The standard gives the
term "undefined behavior" a specific meaning. Using that term to mean
something different is simply confusing. So, please, when you're talking
about the language definition, use the term "undefined behavior" to mean
behavior of an erroneous program for which the standard imposes no
requirements. Use the term "unspecifie d behavior" to mean behavior of a
well-formed program that depends on the implementation (usually there is
an obvious set of alternatives).
Jul 16 '06 #3

av*******@gmail .com wrote:
In "Bjarne Stroustrup's C++ Style and Technique FAQ" at:
http://www.research.att.com/~bs/bs_f...aluation-order

for the statement:
f(v[i],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
as the C++ Standard says:
"5.2.2 Function
The order of evaluation of arguments is unspecified."

So isn't he wrong at this?

I think it is undefined because the value of 'i' is accessed and also
moidified within the sequence point and hence it is undefined.

Sharath A.V
Both he and the standard are saying the same thing.
How can you modify and access a variable simultaneously? Thats
impossible.
You've missunderstood both statements.
What he and the standard are saying is that no restriction is imposed
on the compiler to first evaluate one argument and then the other in
any particular sequence.
Undefined or unspecified: same thing.

I disagree with your intepretation as well.
v[i] is a particular element in a container, the question is which one?
The answer is "it doesn't matter", unspecified, undefined, "who cares",
not guarenteed, etc.

Jul 16 '06 #4
Pete Becker wrote:
James Bannon wrote:
>>>
According to the standard the value of f is unspecified but what does
that mean in practice? Undefined results because we cannot guarantee
what the result of calling f will be.

Well, in a way, yes, but this is sloppy wording. The standard gives the
term "undefined behavior" a specific meaning. Using that term to mean
something different is simply confusing. So, please, when you're talking
about the language definition, use the term "undefined behavior" to mean
behavior of an erroneous program for which the standard imposes no
requirements. Use the term "unspecifie d behavior" to mean behavior of a
well-formed program that depends on the implementation (usually there is
an obvious set of alternatives).
Normally I would quote directly from the Standard as I have a copy on my
bookshelf, that is when I want to be sure I'm not putting in my own
(probably mistaken) interpretation of some clause. However, I think you
have to remember that the audience reading the pages of an FAQ are not
likely to be language specialists and are unlikely to appreciate the
difference between "undefined" and "unspecifie d" especially since both
usually arise from the use of erroneous constructs and both have similar
effects in the sense that "results cannot be relied upon". If you were
teaching a class of learners would you say "the results are unspecified"
or "don't do this because, even though the language allows it, it
doesn't work".

Cheers
Jim.
Jul 16 '06 #5
On 16 Jul 2006 08:30:56 -0700, av*******@gmail .com wrote in
comp.lang.c++:
In "Bjarne Stroustrup's C++ Style and Technique FAQ" at:
http://www.research.att.com/~bs/bs_f...aluation-order

for the statement:
f(v[i],i++);

he says that "the result is undefined because the order of evaluation
of function arguments are undefined".
Bjarne's wording is less than accurate here. Actually, the order of
evaluation of function arguments is unspecified.
But AFAIK, the order of evaluation of function arguments is unspecified
as the C++ Standard says:
"5.2.2 Function
The order of evaluation of arguments is unspecified."

So isn't he wrong at this?
But the first point in his statement, that the result is undefined, is
true. Regardless of the order of evaluation of the function
arguments, 'i' is both modified, and its value read other than for the
purpose of deriving the new value.
I think it is undefined because the value of 'i' is accessed and also
moidified within the sequence point and hence it is undefined.

Sharath A.V
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 16 '06 #6
James Bannon wrote:
According to the standard the value of f is unspecified but what does
that mean in practice?
It means: "Boys, don't try this at home!"

--
Salu2
Jul 16 '06 #7
James Bannon wrote:
Pete Becker wrote:
>James Bannon wrote:
>>>>
According to the standard the value of f is unspecified but what does
that mean in practice? Undefined results because we cannot guarantee
what the result of calling f will be.


Well, in a way, yes, but this is sloppy wording. The standard gives
the term "undefined behavior" a specific meaning. Using that term to
mean something different is simply confusing. So, please, when you're
talking about the language definition, use the term "undefined
behavior" to mean behavior of an erroneous program for which the
standard imposes no requirements. Use the term "unspecifie d behavior"
to mean behavior of a well-formed program that depends on the
implementati on (usually there is an obvious set of alternatives).


Normally I would quote directly from the Standard as I have a copy on my
bookshelf, that is when I want to be sure I'm not putting in my own
(probably mistaken) interpretation of some clause.
The words I used are a paraphrase of the words in the standard.
However, I think you
have to remember that the audience reading the pages of an FAQ are not
likely to be language specialists and are unlikely to appreciate the
difference between "undefined" and "unspecifie d" especially since both
usually arise from the use of erroneous constructs and both have similar
effects in the sense that "results cannot be relied upon". If you were
teaching a class of learners would you say "the results are unspecified"
or "don't do this because, even though the language allows it, it
doesn't work".
I have no problem with someone saying "the results cannot be relied
upon." What I objected to was the switch from technical language
(undefined behavior versus unspecified behavior in the original message)
to non-technical language using some of the same words.
Jul 16 '06 #8
Julián Albo wrote:
James Bannon wrote:
>According to the standard the value of f is unspecified but what does
that mean in practice?

It means: "Boys, don't try this at home!"
Precisely, and undefined behaviour means precisely the same thing from
this point-of-view: don't do it; it doesn't work; career-limiting move;
abuse of the language; bug; or however you want to put it.

Of course, if we're speaking "standardes e" we should be precise because
there is a difference between undefined and unspecified just as there is
a difference between mandated and implementation-defined (IMHO the
latter distinction is more important).

Cheers
Jim
Jul 16 '06 #9
av*******@gmail .com wrote:
But AFAIK, the order of evaluation of function arguments is unspecified
as the C++ Standard says:
"5.2.2 Function
The order of evaluation of arguments is unspecified."

So isn't he wrong at this?
No, because he didn't write that the /behavior/ is undefined, but that
the /order/ is undefined.

This is accurate because it's what unspecified behavior means with
respect to order: the standard does not define the order, nor require
that the implementation document how the order is chosen.

The informal language Stroustrup is using is perfectly adequate for the
purpose of the FAQ.

The reader is already suposed to understand that combining v[i] and i++
in the same expression causes undefined behavior. WIth that additional
example, Stroustrup is illustrating that the undefined behavior cannot
be prevented by separating pieces of the expression into separate
argument expressions, because the order of evaluation of arguments is
not defined either.

Jul 16 '06 #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 ??
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; }
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.)
12
1609
by: Neroku | last post by:
I don't know if the following expression is UB: i=2; x = (i=3) * i; Since in C, evaluation order is unspecified, this expression is 'at least' unspecified, since we don't know which operand evalutes first, (i=3) or i, So, would x be always 6 or 9 ? depending on the evaluation order.
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
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...
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...
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
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.