简体   繁体   中英

invalid operands of types 'volatile uint8_t* and 'uint8_t (*)

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM