c++ - 'volatile uint8_t* and ' uint8_t (*) 类型的无效操作数

标签 c++ arrays pointers g++

我有地址/指针转换的问题

我遵循了 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/

相关文章:

c++ - 类中静态成员的地址

c++ - 取消引用指针到指针

c++ - 固定更新速度

c++ - 警告 : Function return user-defined type which is incompatible with C

c++ - INT_MIN 的用途

javascript - 使用 `array.some()` 而不是 bool 值返回一个元素

c - 为什么我会收到 "assignment from incompatible pointer type"错误?

c++ - 结构中的 vector - 最佳方法? C++

javascript - 对象数组与分隔字符串数组的数组

javascript - 如何从一组日期中获取最新日期? - JavaScript/ typescript