c - How to find memory leaks with Clang -


i have installed clang in machine (ubuntu) in order find memory leaks in c code. wrote sample code in order check working of follows:

/* file: hello.c leak detection */ #include <stdio.h> #include <stdlib.h>  void *x;  int main() {   x = malloc(2);   x = 0; // memory leak   return 0; } 

i found options in internet compile like

$ scan-build clang --analyze hello.c 

and

$ scan-build clang -fsanitize=address hello.c 

but none of them showing signs of memory leak.

scan-build: using '/usr/bin/clang' static analysis
scan-build: removing directory '/tmp/scan-build-2015-07-02-122717-16928-1' because contains no reports.
scan-build: no bugs found.

can kindly tell how correctly use clang memory leak detection.

interestingly, clang static analyzer finds memory leak if declare void *x inside main:

int main() {   void *x = malloc(2);   x = 0; // memory leak   return 0; } 

analyzing code running:

scan-build clang -g hello.c 

gives warning like:

hello.c:9:3: warning: potential leak of memory pointed 'x'   return 0;   ^~~~~~~~ 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -