小编Cod*_*ero的帖子

为什么指针增量语句被优化了?

我已经编写了一种方法来解析GPS中的字符串,但出于某种原因,关键线在不使用时会被优化掉-O0.方法代码如下:

bool Gps::ParsePMTK_ACK(PmtkAck_t &dst, uint8_t *buf, int32_t size)
{    
    // Verify message ID
    uint8_t *pCur = buf;

    memset(&dst, 0, sizeof(dst));

    if (strncmp((char*)pCur, "$PMTK001", 8) != 0) {
        return false;
    }

    pCur = pCur + 8; // <--- This line gets optimized away when not on -O0
                     //      thus causing the pointer to NOT be incremented

    if (*pCur != SEPARATOR) {
        return false;
    }

    ++pCur; // <--- Not optimized away

    if (ProcessInt(dst.cmd, &pCur) != 1) {
        return false;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ embedded optimization

1
推荐指数
1
解决办法
129
查看次数

标签 统计

c++ ×1

embedded ×1

optimization ×1