1

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
    Did you intend the pointer to be volatile, rather than the data it points to? That would be `uint8_t * volatile`. – Mike Seymour Mar 18 '15 at 11:39
  • 1
    Also, you shouldn't use [reserved names](http://stackoverflow.com/questions/228783) like `__usrRxIndex`. – Mike Seymour Mar 18 '15 at 11:40
  • @MikeSeymour Good catch. This is why IMO you should always put modifiers *behind* the thing you're modifying, even if it's correct to place it in front of it. Same holds for `const` of course. Too bad most people don't seem to want to do this consistently :-( – JorenHeit Mar 18 '15 at 11:44
  • I' confused because uint8_t (*)[512] is a pointer to an unit8_t[512], which I didn't find in the code. – user3528438 Mar 18 '15 at 11:47

1 Answers1

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.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • I only put memcpy at begin to show my intention, in real code I check two cases and fill it till APP_RX_DATA_SIZE and then starting from 0. –  Mar 18 '15 at 12:28
  • But the case was: memcpy(UserRxBufferFS+__usrRxIndex, .. i miss that two pointers cant be added –  Mar 18 '15 at 12:29