Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8758645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:36:06+00:00 2026-06-13T14:36:06+00:00

To improve performance in my clique-partitioning program, which uses ordered arrays, I included in

  • 0

To improve performance in my clique-partitioning program, which uses ordered arrays, I included in the stop condition of my for loop an access to an element of the array I’m looping into.

int myValue = 13;

for (int i=0; array[i] < myValue; i++)
{
    //performing operations on the array
}

This is clearly unsafe, since it could be that my array only contains values that are less than myValue, so I tried this

int myValue = 13;

for (int i=0; i < array.size() && array[i] < myValue; i++)
{
    //performing operations on the array
}

In this implementation, all seems to go well, but if I switch the conditions, I fall into the same problem of the first example.

int myValue = 13;

for (int i=0; array[i] < myValue && i < array.size(); i++)
{
    //performing operations on the array
}

So, I deduced that this is clearly due to the way the compiler sets the order of the two conditions, since in the last case, even if I ask to enter the loop only if i is not greater than the size of the array, I’m previously reading a value that could be out of the bounds of the array.

My question is: is it always safe to do as I did in the second implementation, or could the compiler sometimes switch my control conditions leading to unsafe code?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:36:08+00:00Added an answer on June 13, 2026 at 2:36 pm

    The && (logical and) operator always short circuits if it can. Your second example is safe.

    Note, this applies to primitive types only, not those that overload the boolean operators.

    Because no self-respecting C++ answer would be complete without a standard quote:

    5.14.1 (Logical AND)
    The && operator groups left-to-right. The operands are both contextually converted to type bool (Clause 4).
    The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right
    evaluation: the second operand is not evaluated if the first operand is false.

    5.14.2 (Logical OR)
    The || operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). It
    returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right
    evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to improve the performance of a query, which does has a
TickZoom is a very high performance app which uses it's own parallelization library and
We have a C++ application for which we try to improve performance. We identified
I have a very simple program that I am trying to improve performance. One
everyone,recently i was debugging a program for improve performance.i notice a interest thing about
We have a SQL Server 2005 database for which we want to improve performance
I heard a lot about denormalization which was made to improve performance of certain
I am trying to improve performance on an application using Hibernate which is executing
In a C extension to my Python program, I am trying to improve performance
To improve performance of our MVC3 application we want to mark some Controller's to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.