1

I have a coding style/code optimisation related question. I need to declare an array which will contain some sensor data. This array needs to be available in two scopes measurement.c and communication.c.

Can you tell me what option in your opinion is better: 1. When an array is declared globally within measurement.c and then I will use fcn to get a pointer to this array in different scope.

uint8_t* gPtr = (NULL); // initialising as a null pointer

void sensorDriver(void)
{
    static uint8_t arr[10]; // local array
    gPtr = arr; // assigning local address to global pointer

    // some other code which getting and processing data
}

const uint8_t* getArrPtr(void)
{
    return gPtr;
}
  1. When an array is declared locally and I will have a global pointer which will have assigned local array address.
uint8_t arr[10]; // global array

void sensorDriver(void)
{
    // some other code which getting and processing data
}

const uint8_t* getArrPtr(void)
{
    return arr;
}

Data can be received in the same way for two methods: communication.c

void sendData(void)
{
    const uint8_t* arrPtr = getArrPtr();
{
  • I would declare a volatile pointer to the hardware register, then i wouldn't have to do any work retrieving the data from the hardware, i can just dereference the volatile pointer and it automagically accesses the hardware. – Owl Aug 23 '19 at 09:24
  • 2
    All I can say is that you have a fundamentally strange program design. The only part of your program that should be reading incoming sensor data is the driver for whatever peripheral you are using. Get that right first, then take it from there. Avoid mixing up hardware code with application code. – Lundin Aug 23 '19 at 10:57
  • Also, you don't mention double-buffering at all, which is the very first thing to look into. Is it needed, or does the hardware provide mechanisms for that etc. – Lundin Aug 23 '19 at 10:59
  • I was trying to keet example code simple and show any the concept of problem. In fact, you are probably right and using word main in the example is a bit confusing. In a real application, I have a decided buffer for interrupt routine and one more for data processing. – Mateusz Paczyński Aug 23 '19 at 11:15
  • Ok so keep the buffer shared with the ISR local to the driver, and use another buffer which you pass along to the caller through setter/getters. You'll need to protect against race conditions. Buffer swaps can be done by swapping pointers, no need for memcpy etc. – Lundin Aug 23 '19 at 13:43
  • `static uint8_t arr[10]; // local array` is not a local array. Indeed it is a global array not located on the stack but in the data section of the memory. The `static` keyword reduces the scope by hiding the variable name, but the adress of the array is still accessible. So the memory is globaly available but with the static array in the function only accessible via pointer if you are not running code in the same function. – grml Aug 27 '19 at 11:47

1 Answers1

1

You need to consider two things:

(i) The local array has to be protected. [Refer to http://codepad.org/q98gkGY0 for an example of the proper way to protect access to a local array].

(ii) You need to lock the object to prevent a concurrent access during reading/writing.

C. R. Ward
  • 93
  • 5