Proper declaration of array with volatile values inside it?
-
I am programming regular C for an embedded ARM processor. I need to declare an array with volatile elements inside it. The location of the array is constant, it's only the elements inside the array that can change and they are, like I said, volatile. What is the correct way to declare it? A. uint8_t volatile UARTrxBuffer[10]; or B. volatile uint8_t UARTrxBuffer[10]; or C: volatile uint8_t volatile UARTrxBuffer[10];
-
I am programming regular C for an embedded ARM processor. I need to declare an array with volatile elements inside it. The location of the array is constant, it's only the elements inside the array that can change and they are, like I said, volatile. What is the correct way to declare it? A. uint8_t volatile UARTrxBuffer[10]; or B. volatile uint8_t UARTrxBuffer[10]; or C: volatile uint8_t volatile UARTrxBuffer[10];
The general answer is B but in your case there is no difference between A and B, and C would generate a compiler error. It can be better explained when having a dynamically created array:
// my_data1 is a volatile pointer
uint8_t* volatile my_data1 = (uint8_t* volatile)malloc(10);
// The array elements are volatile
volatile uint8_t* my_data2 = (uint8_t*)malloc(10);
// Both are volatile
volatile uint8_t* volatile my_data3 = (uint8_t* volatile)malloc(10);With the volatile pointers, the casts must also contain the
volatile
keyword. But with arrays, the pointer can't be re-assigned so that version C is not allwed and the position of thevolatile
keyword does not care. -
I am programming regular C for an embedded ARM processor. I need to declare an array with volatile elements inside it. The location of the array is constant, it's only the elements inside the array that can change and they are, like I said, volatile. What is the correct way to declare it? A. uint8_t volatile UARTrxBuffer[10]; or B. volatile uint8_t UARTrxBuffer[10]; or C: volatile uint8_t volatile UARTrxBuffer[10];
I am confused what Jochen is saying to you but I will say nothing you nor Jochen have code will make the items within the array volatile. First lets cover the basics volatile is an optimizer instruction it has very little to do with the C language as such it is also HIGHLY vendor sensitive. So the first thing you always need to do is check your vendor tools and specification. My next warning is to always put the volatile between the type and the name not at the front as people love to do. Things like the arm compilers are notorious with volatile in the front of the type and if you use it in macros you will end up with it on the wrong thing. I covered this in a recent article on baremetal with the RPi but Arm covers this in there documentation ARM Information Center[^] You see the format, that is not just the arm standard it is the standard I suggest you use as most embedded compilers get it right
type volatile * const ptr;
| | | | |
| | | | +------> ptr is a
| | | +-----------> constant
| | +---------------> pointer to a
| +---------------------> volatile
+---------------------------> integer/long/etcAlmost all vendor tools will fail to do with volatile what the full C specification says because the specification isn't written by embedded processor vendors and they get little say in it. Some of the junk that is in the C specification around the word volatile is ignored because it doesn't make sense in micro-controllers. The scary stat is 96% of vendor tools fail the full C implementation (Testing result reference :[^]) So lets get down to specifics the volatile must also be on any item within the array putting a volatile on the array itself will not work almost any compiler. The compiler doesn't see "blocks" of things as volatile it only sees things it is accessing in the mode it is accessing them as volatile. The fact you want a volatile inside a malloc would tend to indicate you are playing around with a DMA controller so I will reference hardware access here. If this is just shared multitask memory nothing changes it's just simp