0

I want to know why to declare variables with static volatile and arrays with only static in TWI library?

For example;

static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
static volatile uint8_t twi_masterBufferIndex;
static volatile uint8_t twi_masterBufferLength;
  • Do you understand what volatile does? – Ignacio Vazquez-Abrams Nov 21 '17 at 19:58
  • I learned something like that this variable maybe changed but in which aspect I don't know. – R1S8K Nov 21 '17 at 20:01
4

You use volatile to mark variables that are going to change at any moment, in a ISR (Interrupt Service Routine) typically.

You mark them so the compiler doesn't optimize, and always go and pick its actual value. If you don't do that, then an ISR may update a variable while the main "thread" of execution keeps using its old value. That's is a nasty situation to debug.

You only need to mark variables that are used both in the ISR and in the main sketch. If twi_masterBuffer is not changed inside an ISR, you don't need to mark it with volatile.

static is used inside function to mark variables that need to be permanent, i.e., keep their values between calls, but not accessible from outside that function.

|improve this answer|||||
  • I'm studying the TWI library, to modify my I2C library because my I2C library has a bug, and I went back to learn about the well professional written code for I2C which is the TWI library. So, I'm going through C techniques of coding. And, now this is going more difficult to me because after looking over and over to the code, I looked to the ISR and it has operations of sending and receiving data, then what's going in the actual functions? – R1S8K Nov 21 '17 at 20:08
  • static and volatile are a different aspect of programming, but very related, because I have to understand what actual functions do and what ISR also do, and then I may understand what static and volatile means. – R1S8K Nov 21 '17 at 20:12
  • static variables are declared at the head of the .c code, they are not inside any function, so how would they be used from outside the function? Or, the static goal here is to keep the values of these variables constant between all the functions inside TWI library, right? – R1S8K Nov 21 '17 at 20:14
  • @PerchEagle. You are confusing static with const. – user31481 Nov 21 '17 at 20:19
  • 1
    @PerchEagle. Static global variable means: this variable is global to all functions in the file (.ino) where it's declared, and unknow in other files (.ino). Non-static global variable means they are global across all files in the project. It's not about the value; it's about where it's allocated (heap vs stack), what is his lifetime (forever vs only during that particular function call), and where it's know (one file vs all files in the project). C/C++ is tricky, tricky. – user31481 Nov 21 '17 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.