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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:04:56+00:00 2026-06-10T16:04:56+00:00

I heard about compiler optimizations. For example the one when: while(myBool) doStuff(); compiler knows

  • 0

I heard about compiler optimizations.

For example the one when:

while(myBool)
    doStuff();

compiler knows you do not modify myBool inside a while loop, it just read myBool once and doesn’t check it every time.

This can be affected by volatile keyword right?

So I have tried to “trick” compiler, it thinks value was not changed.

int main()
{
    struct st 
    {
        int a;
        int b; //I want to make difference by writing volatile here
    }s;

    s.b = 1;

    while(s.b > 0)
    {
        *((&s.a)+1) = 0;
        std::cout << "test" << std::endl;
    }
}

But even with full optimizations turned on (vs2012), this does not trick the compiler. This is probably very lame trick 🙂

How can I trick it? Is it even possible?

My goal is to create simple single-threaded program, which prints “test” with volatile keyword used and prints infinite-times “test” without the keyword.

EDIT: Unfortunately I am not good with assembler, so I really can not read if the memory reading was optimized out in the first place 🙂

  • 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-10T16:04:58+00:00Added an answer on June 10, 2026 at 4:04 pm

    What you’re doing is undefined behavior. You can’t access s.b by writing to *((&s.a)+1) instead since there’s no guarantee that s.a and s.b don’t have any padding bytes between them.

    If you want to force there to be no padding, look into your compiler options. For GCC/Clang, you’d declare the structure with __attribute__((packed)). For MSVC, you’d use the #pragma pack directive.

    There’s no good reason to be accessing s.b through s.a using that code. What you should really do in this case, assuming you have a good reason for not accessing s.b directly, is use an array (volatile, if need be). Arrays are guaranteed to be contiguous in memory without needing special attributes/pragmas.

    Furthermore, the compiler won’t make certain optimizations in some cases: if i is a variable and you write to a[i] followed by a read of a[1], the compiler can’t assume that a[1] wasn’t just written to, so it won’t be able to cache it in a register. Whereas if you have two unrelated variables a and b and you write to *(&a+1) and then read from b, the compiler will assume that b was not just written to. That’s one very good reason why *(&a+1) is undefined behavior, since it leads the compiler to make untrue assumptions and cause your program to behave in strange ways.

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

Sidebar

Related Questions

I heard about the new entity framework for .net, and decided to modify my
I just heard about that x mod (2^32-1) and x / (2^32-1) would be
I see an F# compiler with MS Visual Studio 2010, I heard about J#
I heard about gems like faker or populator but they are a little bit
I heard about the chicken and the egg and bootstrapping. I have a few
I heard about B-Method which is invented in France. Is it an alternative to
I've heard about how base classes and method overriding can be combined to eliminate
I've recently heard about Qt and read about how fun it is to develop
I'v heard about aspects in a programming language and I was pretty enthusiastic about
I've heard about many application developers having a bit of trouble in regards 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.