GCC warning for non-running heap blocks

So the question is simple, is there a way to tell GCC that I want to receive a warning if I do not free the block allocated for the heap? I know that we may have proprietary blocks for some purposes / we have already reached the end of the program or something like that.

int main(){ int *a = malloc(sizeof(int)); return 0; } 

If I can get a warning even for this, it will be awesome.

+7
c gcc gcc-warning
source share
2 answers

This is not a possible job for GCC. Static analysis cannot prove that it is free to forget that the work of runtime analyzers like memcheck valgrind , or, ultimately, gcc -fsanitize=leak , which I haven’t seen there so far , only with clang -fsanitize = leak .

But you will not get a compilation warning even when gcc or clang supports it. This will be a warning at runtime.

+3
source share

The compiler cannot predict and warn for unblocked blocks. This is runtime work, not compile time. You can implement your own subsystem without checking malloc or modify the memory management library.

+2
source share

All Articles