我有地址/指针转换的问题
我遵循了 OOTB recive (uint8_t* Buf, uint32_t *Len) 的代码;接收到数据中断时异步运行的函数
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;
}
}
然后我尝试通过 volatile __usrRxIndex 捕获数据的结尾,每次捕获数据时它都会更新。
但是当我尝试使用 g++ 编译器编译它时出现错误:
error: invalid operands of types 'volatile uint8_t* {aka volatile unsigned char*}' and 'uint8_t (*)[512] {aka unsigned char (*)[512]}' to binary 'operator-'
我做了一些解决方法并将每个地址计算为数字然后进行减法,但我的问题是为什么 g++ 编译器不允许数组和指针减法?有什么简单的方法可以避免出现此错误吗?
最佳答案
在
memcpy(UserRxBufferFS+__usrRxIndex, Buf, *Len);
该添加不应编译并且没有意义。你可能想要:
memcpy(__usrRxIndex, Buf, *Len);
另一件事是您在 memcpy
已经损坏的内存之后检测到缓冲区溢出。它需要在memcpy
之前检测缓冲区溢出。
关于c++ - 'volatile uint8_t* and ' uint8_t (*) 类型的无效操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29121003/