Inspired by this post in a recent discussion also involving leaks and loops I tested if a leak is detected when the pointer returned from malloc is stored in a member of a struct. Indeed, in the following code cppcheck detects a memory leak: int main() { for (int n=0; n < 1; n++) { }; struct { char * buf; } test; test.buf = malloc(10); return 0; }
Using the snippet below cppcheck reports 'memleak' in addition to 'unreadVariable' and 'unusedAllocatedMemory': int main() { char * buf = malloc(10); return 0; } cppcheck testmemleak.c --enable=all --inconclusive But 'memleak' is no longer reported when the code includes a for loop: int main() { for (int n=0; n < 1; n++) { } char * buf = malloc(10); return 0; } 'unreadVariable' and 'unusedAllocatedMemory' are still reported however. Is this a bug or am I missing something obvious? Same behavior using...