C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Q: sequence of excution in if

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
zl2k
Guest





PostPosted: Mon Jul 24, 2006 7:23 am    Post subject: Q: sequence of excution in if Reply with quote



hi, all
If I am using gcc, is the sequence of excution in "if" the same as what
I wrote? Here is an example:

vector<int> v;
unsigned short a;
.....
if (a < v.size() && v[a] > 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a > v.size(). But I am not sure
if gcc will do in this way. Thanks for your comments.

zl2k
Back to top
Alf P. Steinbach
Guest





PostPosted: Mon Jul 24, 2006 7:40 am    Post subject: Re: Q: sequence of excution in if Reply with quote



* zl2k:
Quote:
hi, all
If I am using gcc, is the sequence of excution in "if" the same as what
I wrote? Here is an example:

vector<int> v;
unsigned short a;
....
if (a < v.size() && v[a] > 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a > v.size(). But I am not sure
if gcc will do in this way. Thanks for your comments.

The built-in && introduces a sequence point. The left expression is
completely evaluated before the right one is evaluated, which it is if
and only if the left expression evaluates to non-zero.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Daniel T.
Guest





PostPosted: Mon Jul 24, 2006 7:44 am    Post subject: Re: Q: sequence of excution in if Reply with quote



In article <1153707803.978860.275850 (AT) b28g2000cwb (DOT) googlegroups.com>,
"zl2k" <kdsfinger (AT) gmail (DOT) com> wrote:

Quote:
hi, all
If I am using gcc, is the sequence of excution in "if" the same as what
I wrote? Here is an example:

vector<int> v;
unsigned short a;
....
if (a < v.size() && v[a] > 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a > v.size(). But I am not sure
if gcc will do in this way. Thanks for your comments.

Yes it will. It's a requirement of the && operator (except when it is
overridden.)
Back to top
Kai-Uwe Bux
Guest





PostPosted: Mon Jul 24, 2006 7:46 am    Post subject: Re: Q: sequence of excution in if Reply with quote

zl2k wrote:

Quote:
vector<int> v;
unsigned short a;
....
if (a < v.size() && v[a] > 0)
cout<<v[a];

I expect that the "a < v.size()" will excute first so that I won't get
into the segmentation error in cas of a > v.size(). But I am not sure
if gcc will do in this way.

Any standard compliant compiler will do it the way you expected. As far as I
know, gcc is compliant in this regard.



Best

Kai-Uwe Bux
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.