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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:07:49+00:00 2026-06-01T03:07:49+00:00

volatile is to tell the compiler not to optimize the reference, so that every

  • 0

volatile is to tell the compiler not to optimize the reference, so that every read/write does not use the value stored in register but does a real memory access. I can understand it is useful for some ordinary variable, but don’t understand how volatile affects a pointer.

volatile int *p = some_addr;
int a = *p; // CPU always has to load the address, then does a memory access anyway, right?

What is the difference if it has been declared as int *p = some_addr?

  • 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-01T03:07:50+00:00Added an answer on June 1, 2026 at 3:07 am

    A pointer of the form

    volatile int* p;
    

    is a pointer to an int that the compiler will treat as volatile. This means that the compiler will assume that it is possible for the variable that p is pointing at to have changed even if there is nothing in the source code to suggest that this might occur. For example, if I set p to point to a regular integer, then every time I read or write *p , the compiler is aware that the value may have changed unexpectedly.

    There is one more use case for a volatile int*: If you declare an int as volatile, then you should not point at it with a regular int*. For example, this is a bad idea:

    volatile int myVolatileInt;
    int* ptr = &myVolatileInt; // Bad idea!
    

    The reason for this is that the C compiler no longer remembers that the variable pointed at by ptr is volatile, so it might cache the value of *ptr in a register incorrectly. In fact, in C++, the above code is an error. Instead, you should write:

    volatile int myVolatileInt;
    volatile int* ptr = &myVolatileInt; // Much better!
    

    Now, the compiler remembers that ptr points at a volatile int, so it won’t (or shouldn’t!) try to optimize accesses through *ptr.

    One last detail – the pointer you discussed is a pointer to a volatile int. You can also do this:

    int* volatile ptr;
    

    This says that the pointer itself is volatile, which means that the compiler shouldn’t try to cache the pointer in memory or try to optimize the pointer value because the pointer itself might get reassigned by something else (hardware, etc.) You can combine these together if you’d like to get this beast:

    volatile int* volatile ptr;
    

    This says that both the pointer and the pointee could get changed unexpectedly.The compiler can’t optimize the pointer itself, and it can’t optimize what’s being pointed at.

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

Sidebar

Related Questions

Marking a variable as volatile in Java ensures that every thread sees the value
I understand the volatile keyword in Java can make the read/write operations of reference
Is there a way to tell the compiler to not optimize selective portions of
While doing concurrent programming I need to tell the compiler/optimizer that it may not
I read some articles about the volatile keyword but I could not figure out
I know that by the volatile keyword, volatile int k=7; we hint the compiler
I have read When to use 'volatile' in Java? but I'm still confused. How
Some languages provide a volatile modifier that is described as performing a read memory
I'm aware of the use of volatile in Java. That is (based on the
What does AtomicBoolean do that a volatile boolean cannot achieve?

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.