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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:54:00+00:00 2026-06-17T08:54:00+00:00

What is extern volatile pointer. extern volatile uint32 *ptr; Here, what will be the

  • 0

What is extern volatile pointer.

extern volatile uint32 *ptr;

Here, what will be the behavior of *ptr?
What does this actually mean?

And, when it should be used?

I have tried to google about it, but didn’t get any satisfactory answer, there is not much info present about this combination.

  • 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-17T08:54:00+00:00Added an answer on June 17, 2026 at 8:54 am

    Both extern and volatile keywords can be considered independently. The role of each of these keywords does not interact with the other one, and thus an explanation of each of them can be detailed independently, as below.

    extern tells the compiler that the actual definition of ptr is in another module (another .c). Basically, there is no much change to how the compiler handles ptr – having extern just tells the compiler that it does not have to reserve some space in memory for ptr as it is done elsewhere in another .c, and its actual memory location will be given by the linker later.

      extern uint32 *ptr;
    

    If you omit extern the compiler will not complain. However, later, the linker, when it tries to link all object modules in order to build the final executable program, will throw an error saying “ptr is defined twice” (since it is already defined in another .c).

      uint32 *ptr;
    

    volatile tells the compiler that the memory location where ptr resides may be altered / modified by some external event, and it (the compiler) should not rely on some efficiency optimizations like considering that the value of ptr will not change during the scope of a few sequential lines of C. Such an event may be an asynchronous interruption, that happens when the CPU executes the above said scope, and modifies the ptr value.

    Typically (with virtual assembly code of optimized C in comments), REGx are CPU registers, and we are not much interested in the variable y…

      int x = 10;
    
      int func() {
        int y;              // REG4
        printf("%d\n", x);  // Set REG3 = memory(x) and display x
        x += 2;             // Add 2 to REG3
        y = x * x;          // REG4 = REG3 * REG3
        printf("%d %d\n", x, y); // Do printf(..., REG3, REG4)
        x += 5;             // REG3 = REG3 + 5
                            // memory(x) = REG3 (save register to memory)
        return y;           // return REG4
      }
    

    should display 10, 12, 144. For the sake of efficiency (memory accesses are more costly than register accesses) say the compiler stores the value of x in an internal CPU register (REG3) and uses it in func safely until the end where it stores the new value of x (it’s a global var) to x memory location. x is 17 at the end.

    But imagine the program is more complex than that and has a clock interruption every minute that subtract 10 to x. What happens if an interruption…

      void inter_call_by_timer_every_minute() {
         x -= 10;
      }
    

    … occurs in func say just after the printf("%d\n", x); line? func has x in REG3 (10), add 2 (12) and finally add 5 (17) and stores the REG3 result to x memory location (17). This is wrong as the interruption effect (-10) has been hidden by the compiler optimization, since it stores the value from REG3 to memory(x) at the end ignoring the subtract done by the interruption. The correct result was: x is initially 10, interrupt subtract 10 to it (0) after the first printf in func, then 2 is added, then 5. Result 7.

    Adding volatile

      volatile int x = 10;
    

    will have the compiler avoid the x optimizations in func

      int func() {
        int y;              // REG4
        printf("%d\n", x);  // display memory(x)
        x += 2;             // memory(x) += 2
        y = x * x;          // REG4 = memory(x) * memory(x)
        printf("%d %d\n", x, y); // Do printf(..., memory(x), REG4)
        x += 5;             // memory(x) += 5
        return y;           // return REG4
      }
    

    and read the value of x from memory all the time. Result, having an interruption from inter_call_by_timer_every_minute after the first printf, is x == 7.

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

Sidebar

Related Questions

Why does gcc allow extern declarations of type void? Is this an extension or
I have the function prototype here: extern C void __stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); I need to
What does the extern keyword mean? I've seen that in front of an function
How does the following example usage of extern specifer behave. We have a global
Have: [DllImport(OpenAL32.dll)] static extern void alcOpenDevice(char*[] devicename); Wanna send the name to this function
Consider this C code: extern volatile int hardware_reg; void f(const void *src, size_t len)
I have this C++ code: extern C __declspec(dllexport) VOID AllocateFoo(MY_DATA_STRUCTURE** foo) { *foo =
What exactly does putting extern C into C++ code do? For example: extern C
I have some extern 'd variables in a namespace in a header file, and
Any idea why this code: extern C __declspec(dllexport) void Transform(double x[], double y[], int

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.