Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Proper declaration of array with volatile values inside it?

Proper declaration of array with volatile values inside it?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhardwaredata-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    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];

    J L 2 Replies Last reply
    0
    • A arnold_w

      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];

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      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 the volatile keyword does not care.

      1 Reply Last reply
      0
      • A arnold_w

        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];

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        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/etc

        Almost 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups