问题描述
我有地址/指针转换的问题
I have problem with address/pointer conversion
我曾跟随code具有开箱即用的recive(uint8_t有* BUF,uint32_t的* LEN);功能,当数据被中断了recived异步拼命地跑
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;
}
}
然后我试图捕捉挥发性__usrRxIndex数据结束,即每当数据被捕获时更新。
Then I trying to catch end of data by volatile __usrRxIndex, that is updated every time when data are captured.
但是,当我试图编译这个使用G ++ compilator我得到了错误:
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-'
我做了一些解决方法,并计算出每个地址号码,然后做减法,但我的问题是,为什么G ++ compilator不要让数组和指针减法?一些简单的方法来没有得到这个错误?
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?
推荐答案
在
memcpy(UserRxBufferFS+__usrRxIndex, Buf, *Len);
这除了不应该将其编译并没有什么意义。你可能想:
That addition should not compile and it does not make sense. You probably want:
memcpy(__usrRxIndex, Buf, *Len);
另一件事是,你发现后的memcpy
缓冲区溢出已经损坏的内存。它需要在的memcpy
来检测缓冲区溢出。
Another thing is that you detect buffer overflow after memcpy
already corrupted memory. It needs to detect the buffer overflow before memcpy
.
这篇关于类型无效的操作数的挥发性uint8_t有*和'uint8_t有(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!