Constant pointer to struct volatile

0

In my implementation, I use a pointer to a struct that can be modified at any time from my code ; I want that, from the exterior of my code, that pointer can be accessed, but only for reading (to be able to use it in certain functions of utility).

struct Thread;

extern volatile struct Thread *const CurrentThread;
  • It needs to be volatile because it will be changed from a interrupt handler (for now, only for Unix), and I want that the compiler knows.
  • It's critical (many functions will use that pointer, maybe from the interrupt handler itself), so in principle I do not want to use a function that return a copy of it (to guarantee read only ).
  • That pointer selects a task from among a set of them. An uncontrolled modification will produce effects, at least, curious .
  • It is always possible to cast, but at least the compiler warns if you try to change its value inadvertently .

    The questions are:

    • Is that statement correct to achieve the objectives described? ?
    • If the answer is negative, how can the desired effects be achieved?

    EDITO - To leave the question somewhat clearer and more specific:

    Pointer to struct :

    struct Thread *
    

    Pointer constant to struct (the pointer itself can not be modified):

    struct Thread *const
    

    Pointer constant to struct . We can not change the value of the pointer, but it can change at any time outside the compiler's control:

    volatile struct Thread *const
    

    Is this last correct?

        
    asked by Trauma 16.12.2016 в 11:59
    source

    2 answers

    1

    There is no mechanism to prevent that memory position or the adjacent ones from being manipulated from outside (otherwise, vulnerabilities would not exist from buffer overflow).

    What you can do is provide a copy of the object

    extern volatile struct Thread *const CurrentThread;
    
    struct Thread CopiaDelObjeto()
    {
      return *CurrentThread;
    };
    

    When returning the item by value, any change made to it will be local and will not be reflected in your own object. This works well until the structure has internal pointers. In this case you can choose to create a reduced version of the structure that does not include pointers and return an object of this second type.

    The first solution will not work either if your idea is that whoever takes a copy of the pointer uses it as an observer . In this case the best thing would be to establish a warning system (standard type Observer or with events) so that anyone who wants to be aware of the evolution of the object will simply have to subscribe.

        
    answered by 16.12.2016 в 12:12
    1

    One of the things I've learned from C is that modifiers (volatile, register, inline ...) often do not mean what they should, and compilers even ignore them.

    With volatile , as far as I know, you prevent the compiler from assuming things to optimize the code. Actually, any data could be modified by another code that you do not consider, even if it is not marked as volatile .

    To the first question I do not know how to answer you, but to hide in C the content of the structures I have always done the following.

    Alternatively you can declare the pointer as void* . In this way, the user has to, necessarily, convert it to the structure to write. To read you can offer functions that receive that void* (because you know the configuration of the structure), and return the data read.

        
    answered by 16.12.2016 в 13:16