In Linux, I have following instructions from main() function:
When I run this program in valgrind to check the memory leaks, it reports as follows:Code:#include <stdio.h> char* testFn1(const char* sampleString) { } int main() { char *inputString, *outString; inputString = malloc(13*sizeof(char)); //line number 41 inputString = "Sample Input"; outString = testFn1(inputString); free(inputString); }
==22554== 1 errors in context 1 of 1:
==22554== Invalid free() / delete / delete[]
==22554== at 0x4004EFA: free (vg_replace_malloc.c:235)
==22554== by 0x80486C5: main (ex54.c:50)
==22554== Address 0x80488CD is not stack'd, malloc'd or (recently) free'd
--22554--
--22554-- supp: 12 Ubuntu-stripped-ld.so
==22554==
==22554== IN SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
==22554==
==22554== malloc/free: in use at exit: 13 bytes in 1 blocks.
==22554== malloc/free: 3 allocs, 3 frees, 32 bytes allocated.
==22554==
==22554== searching for pointers to 1 not-freed blocks.
==22554== checked 47,188 bytes.
==22554==
==22554==
==22554== 13 bytes in 1 blocks are definitely lost in loss record 1 of 1
==22554== at 0x4004405: malloc (vg_replace_malloc.c:149)
==22554== by 0x8048660: main (ex54.c:41)
==22554==
==22554== LEAK SUMMARY:
==22554== definitely lost: 13 bytes in 1 blocks.
==22554== possibly lost: 0 bytes in 0 blocks.
==22554== still reachable: 0 bytes in 0 blocks.
==22554== suppressed: 0 bytes in 0 blocks.
--22554-- memcheck: sanity checks: 0 cheap, 1 expensive
I don't understand why the malloc and free of inputString are reported as problematic. What is wrong in those statements. How to correct them?