'static volatile' vs. 'static' vs. 'volatile' in C
Asked Answered
H

4

12

What's the difference between using the variable specifiers static volatile combined? Or using one alone; like static or volatile in microcontroller programming?

Halothane answered 2/9, 2017 at 4:19 Comment(4)
You may want to reference C11 Standard (draft n1570) § 6.2.2 Linkages of identifiers followed by § 6.2.4 Storage durations of objects and § 6.7.1 Storage-class specifiersFalconiform
These are keywords, datatypes and qualifiers. If you understand them individually, would help you understand what they mean and how can they be used. May be exercise for you :)Jandy
@JohnBollinger thank you for valuing my question overall even it's wasn't well structured. Anyway it's been more than 2 years and I started to understand these specifiers. It's really that I won't get a deep understanding until I get into a program that need me to use them as an alternative way or a must one ! The main idea I learned is that with more advanced codes, I learn new stuff.Halothane
@DavidC.Rankin Thanks for the reference.Halothane
C
20

static - in this case makes the variable visible only inside the current file. static object have static storage duration.

volatile - it is information for the compiler that the object can be changed by something outside the normal execution path (for example, the interrupt routine) and guarantees that the variable will be read before any use and written after every change. volatile (which is a very common misunderstanding) does not guarantee anything else - no atomicity, no cache coherency, etc., etc.

Cns answered 2/9, 2017 at 8:57 Comment(0)
I
8

Many good answers were provided here, but no mention of scope.

Static variables once initialized and later changed within a scope, they retain the changes and never be destroyed or initialized again especially when leaving the scope. Not unless dictated in code. You can say, static variables resemble global variables in terms of their lifecycle but can only be accessed throughout their own scope.

The volatile part has the tendency to force execution to fetch a variable from RAM and not the cached copy in registers or flash. Suppose for example a certain code was submitted to the compiler under certain level of optimization setting. The compiler does not assume any further conditions are attached to variables other than to clear them when they are not used or outside their scope. There are inherently dual uses for volatile, either to disregard optimization offered by the compiler for that variable, or to refrain from using the prefetched copy of that variable except for the one in RAM.

The static volatile is the combination of both behaviors, persistence of that variable in RAM beyond any optimization.

Potential areas of application:

  • Flash programming
  • Cyclical buffers
  • Ring buffers
  • Concurrency and multiprocessing/multithreading
Inimitable answered 3/4, 2022 at 3:34 Comment(0)
H
6

static:

A static variable refers to a class variable that's shared among all instances.

volatile:

Volatile variables are those which are read and written to main memory. They aren't stored in local cache and are always fetched from main memory.

For example, two threads use, say, private volatile int x;. If thread A write(x) and thread B read(x) then, both the times it will write and read from main memory, without using the threads' local cache.

static volatile:

Even if the static variables are shared variables, but in different thread there can be different values for a static variable in the local cache of a thread. To make it consistent for all threads, just declare it as static volatile. So each time it will fetch from main memory.

Hootchykootchy answered 16/1, 2020 at 8:2 Comment(2)
This question is tagged "C". static in C has nothing to do with classes.Flaunch
What does classes have anything to do with static ?? This is a bit misleading.Screwdriver
S
2

For the keywords static and volatile there is written enough...

See for example:

In the concern of the TWI interface, volatile is needed, because functions which modify these variables could be called from different interrupt service handlers. If volatile would be removed, the compiler will optimize code, not knowing that code can be interrupted. That may lead to failures.

Sb answered 2/9, 2017 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.