本文介绍了Cppcheck可能的空指针解引用:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是使用cppcheck代码是正常工作只是cppcheck给出这个错误。

i am just using cppcheck the code is working properly just cppcheck gives this errors.

void WorkerThread(WorkBuffer* m_buffer)
{
    std::cout << "Thread : " << m_buffer->m_id << ".....Starting" << std::endl;

    if (NULL == m_buffer)
        std::cout << "Thread : " << m_buffer->m_id << "......work buffer is null" << std::endl;


    while(!shut_down_flag)
    {
        int k = 0;
        //Sleep(1);
        SleepSystemUsec(100000);
        std::cout << "Thread : " << m_buffer->m_id << "....in while loop" << std::endl;
    } // of while(!shut_down_flag)

    std::cout << "Thread : " << m_buffer->m_id << ".....Request from main thread so ending working thread ...." << std::endl;
};

错误::可能的空指针解引用:m_buffer - 否则它是多余的, / p>

error : : Possible null pointer dereference: m_buffer - otherwise it is redundant to check it against null.

推荐答案

if (NULL == m_buffer)

确保 m_buffer NULL ,然后你用

std::cout << "Thread : " << m_buffer->m_id << "......work buffer is null" << std::endl;
                            ^^^^^^^^^^^^^^^

只有当 m_buffer 不是 NULL (更准确地说,正确构造的 WorkBuffer )。

this, which is only legal if m_buffer is not NULL (more precisely, only if it points to a correctly constructed WorkBuffer).

如果 NULL 是您的函数的可能输入,您需要先检查之前第一个解引用,然后使其指向某个有效的东西,或离开该函数而不解除引用。

If NULL is a possible input for your function, you need to check for it before the very first dereference and then either make it point to something valid or leave the function without dereferencing.

这篇关于Cppcheck可能的空指针解引用:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:29