Menu

sizeof(pointer) not detected

2018-05-23
2018-06-02
  • Sara Wolfie

    Sara Wolfie - 2018-05-23

    The documentation says it should check for "using sizeof(pointer) instead of the size of pointed data", so why doesn't this code get flagged?

    int main(void)
    {
    char junk;
    char * pJunk = &junk;
    unsigned long i;

    i = sizeof(pJunk);
    i += sizeof(&junk);
    
    return i;
    

    }

    I used: cppcheck --enable=all test.c
    (using cppcheck 1.83)

     
  • Daniel Marjamäki

    sorry for late reply. Sometimes people do use sizeof(pointer) by intention. So we can't warn whenever there is sizeof(pointer) we only try to detect when you do it by mistake.

    In your code example I don't think it looks like a mistake.

    We should warn in such cases:

    memset(pJunk, 0, sizeof(pJunk));
    

    or :

    p = (abc *)malloc(sizeof(abc *));
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.