| View previous topic :: View next topic |
| Author |
Message |
zl2k Guest
|
Posted: Mon Jul 24, 2006 7:23 am Post subject: Q: sequence of excution in if |
|
|
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
|
Posted: Mon Jul 24, 2006 7:40 am Post subject: Re: Q: sequence of excution in if |
|
|
* 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
|
Posted: Mon Jul 24, 2006 7:44 am Post subject: Re: Q: sequence of excution in if |
|
|
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
|
Posted: Mon Jul 24, 2006 7:46 am Post subject: Re: Q: sequence of excution in if |
|
|
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 |
|
 |
|