invalid operands of types 'volatile uint8_t* a

2019-08-06 05:41发布

问题:

I have problem with address/pointer conversion

I had followed code with OOTB recive (uint8_t* Buf, uint32_t *Len); function, that is runned asynchronously when data interrupt is recived

uint8_t UserRxBufferFS[APP_RX_DATA_SIZE]; //static buffer 

volatile uint8_t * __usrRxIndex = UserRxBufferFS; //volatile pointer to data end


static int8_t recive (uint8_t* Buf, uint32_t *Len)
{
    uint8_t result = USBD_OK;
    //copy *Len number of data from Buf to UserRxBufferFS 
    memcpy(UserRxBufferFS+__usrRxIndex, Buf, *Len);

    if( (uint32_t)( (__usrRxIndex )- UserRxBufferFS) + *Len > 0xff){ //calculate buffer overflow
        __usrRxIndex = UserRxBufferFS; //buffer
    } else {
        __usrRxIndex += *Len;
    }
} 

Then I trying to catch end of data by volatile __usrRxIndex, that is updated every time when data are captured.

But When I trying to compile this using g++ compilator I got error:

error: invalid operands of types 'volatile uint8_t* {aka volatile unsigned char*}' and 'uint8_t (*)[512] {aka unsigned char (*)[512]}' to binary 'operator-'

I made some workaround and calculate each address as number and then do substraction but my question is why g++ compilator dont allow arrays and pointer substraction? Is some easy way to not get this error?

回答1:

In

memcpy(UserRxBufferFS+__usrRxIndex, Buf, *Len);

That addition should not compile and it does not make sense. You probably want:

memcpy(__usrRxIndex, Buf, *Len);

Another thing is that you detect buffer overflow after memcpy already corrupted memory. It needs to detect the buffer overflow before memcpy.