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 1030285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:40:32+00:00 2026-05-16T12:40:32+00:00

With these two questions as background ( first and second ), I got curious

  • 0

With these two questions as background (first and second), I got curious about how much optimization a C++ compiler can perform when dealing with pointers? More specifically, I’m interested in how smart a compiler is when it optimizes away code which it may detect will never be run.

(Some may point out that this is a dupe of this question, which it may be, but this particular part of it was not entirely answered. So therefore I decided to start a new question which deals only with this concern.)

(I’m not a C++ expert, so I may be wrong in the following statements, but I’ll give it a shot anyway). A C++ compiler may optimize away portions of code which it will recognize never to be executed or never exited (such as loops). Here is an example:


void test() {
    bool escape = false;

    while ( !escape ); // Will never be exited

    // Do something useful after having escaped
}

The compiler would most likely recognize that the loop will never be exited as the code never changes the value of escape so that the loop is exited. This makes the loop useless.

Now, if we were to change the variable to a pointer, would the compiler still optimize away the loop? Say the code looks like so:


void test( bool* escape ) {
    while ( *escape ); // Will this be executed?

    // Do something useful after having escaped
}

My suspicion is that the compiler will do away with the loop, or else the keyword volatile would be redundant, yes?. But what about when working with threads — where it is in fact modified but outside the function, and maybe even outside that C++ file entirely — would the compiler still remove the loop? Does it make a difference if the variable pointed to by escape is a global variable, or a local variable inside another function? Can the compiler make this detection? In this question, some say that the compiler will not optimize the loop if library functions are invoked inside the loop. What mechanisms are then in place when using library functions which prevent this optimization?

  • 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-05-16T12:40:33+00:00Added an answer on May 16, 2026 at 12:40 pm

    In the first case (while ( !escape );) the compiler will treat that as label: goto label; and omit everything after it (and probably give you a warning).

    In the second case (while ( *escape );), the compiler has no way of knowing if *escape will be true or false when run, so it has to do the comaprision and loop or not. Note however, it only has to read the value from *escape once, i.e., it could treat that as:

     bool b = *escape;
     label:   if (b) goto label;
    

    volatile will force it to read the value from ‘*escape’ each time through the loop.

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

Sidebar

Related Questions

I have two noobish questions about Flash, Actionscript, Flex etc. 1) With these technologies
There are two questions to this: How can I best spread the word about
First a little background about why I'm asking this question... I'm working on a
I have two questions concerning fitting a gauss curve to histogram peaks. My first
I have two questions. Before detailing these questions, I want to add I'm using
There are two similar questions asked here and here but no adequate answers are
Two questions, really: Is there a standard/convention regarding the placement on configuration files? For
Actually I have two questions. (1) Is there any reduction in processing power or
Here is the question: write a method that swaps two variables. These two variables
These two methods appear to behave the same to me public IEnumerable<string> GetNothing() {

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.