I can post the output of valgrind but for that I have to rerun the code that I pasted above. Another problem is that the valgrind output would be very long. but still i'll post a truncated version of it for others to see and learn.
---------- Post updated at 04:57 PM ---------- Previous update was at 04:08 PM ----------
First of all, this is the output of valgrind that I get for the original source code that I pasted here which had memory leak. I did not run the program until the end as the kernel will kill it. So, I ran the program for some time and killed it myself.
Code :
==20843== Memcheck, a memory error detector
==20843== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==20843== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==20843== Command: ./a.out
==20843== Parent PID: 20744
==20843==
==20843== Invalid read of size 8
==20843== at 0x3A05862397: getdelim (in /lib64/libc-2.5.so)
==20843== by 0x40145C: read_text_file (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x400E4B: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== Address 0x5ef9b70 is 0 bytes after a block of size 6,556,464 alloc'd
==20843== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843== by 0x400BAD: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843== Invalid write of size 8
==20843== at 0x3A058624D6: getdelim (in /lib64/libc-2.5.so)
==20843== by 0x40145C: read_text_file (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x400E4B: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== Address 0x5ef9b70 is 0 bytes after a block of size 6,556,464 alloc'd
==20843== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843== by 0x400BAD: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843== Conditional jump or move depends on uninitialised value(s)
==20843== at 0x4A0759A: strcat (mc_replace_strmem.c:174)
==20843== by 0x4015B9: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843== Invalid write of size 1
==20843== at 0x4A075B7: strcat (mc_replace_strmem.c:174)
==20843== by 0x4015C7: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20843== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843== Invalid write of size 1
==20843== at 0x4A075C9: strcat (mc_replace_strmem.c:174)
==20843== by 0x4015C7: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== Address 0x54d83c5 is 1 bytes after a block of size 4 alloc'd
==20843== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843== Invalid read of size 1
==20843== at 0x4A075BD: strcat (mc_replace_strmem.c:174)
==20843== by 0x4015D4: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20843== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843== Invalid write of size 1
==20843== at 0x4A075B7: strcat (mc_replace_strmem.c:174)
==20843== by 0x4015C7: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x401143: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20843== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843== Invalid write of size 1
==20843== at 0x4A075C9: strcat (mc_replace_strmem.c:174)
==20843== by 0x4015C7: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x401143: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== Address 0x54d83c5 is 1 bytes after a block of size 4 alloc'd
==20843== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843== Invalid read of size 1
==20843== at 0x4A075BD: strcat (mc_replace_strmem.c:174)
==20843== by 0x4015D4: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== by 0x401143: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843== Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20843== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20843== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20843==
==20843==
==20843== HEAP SUMMARY:
==20843== in use at exit: 181,687,226 bytes in 3,493,754 blocks
==20843== total heap usage: 3,494,530 allocs, 776 frees, 201,796,962 bytes allocated
==20843==
==20843== LEAK SUMMARY:
==20843== definitely lost: 61,943,610 bytes in 725,020 blocks
==20843== indirectly lost: 0 bytes in 0 blocks
==20843== possibly lost: 0 bytes in 0 blocks
==20843== still reachable: 119,743,616 bytes in 2,768,734 blocks
==20843== suppressed: 0 bytes in 0 blocks
==20843== Rerun with --leak-check=full to see details of leaked memory
==20843==
==20843== For counts of detected and suppressed errors, rerun with: -v
==20843== Use --track-origins=yes to see where uninitialised values come from
==20843== ERROR SUMMARY: 5751 errors from 9 contexts (suppressed: 4 from 4)
But now a twist in this entire story. What I did now is this:
Code :
for ( j = 0 ; j < MAXIMUM_WORDS ; j ++ )
{
memset ( words_from_web_page [ j ] , 0 , strlen ( words_from_web_page [ j ] ) );
}
But previoulsy it was, as I first posted it:
Code :
memset ( words_from_web_page , 0 , MAXIMUM_WORDS );
Now when I introduce this change, I see that no memory leak occurs or better memory usage does not increase with time as I see it using the top command using the same function which I posted here first - that is:
Code :
void read_text_file ( FILE *file_pointer , char ** words_to_be_read , unsigned long int *number_of_words )
{
unsigned long int len = 0;
char *line = NULL;
while ( ! feof ( file_pointer ) )
{
getline ( &words_to_be_read [ ( *number_of_words) ++ ] , &len , file_pointer );
}
( *number_of_words )--;
fclose ( file_pointer );
}
This means that problem was not with this function but the probelm was with the way I was using memset(). So, fundamentally I used getline() correctly. I really will loose a lot of hair thinking about this now.
Here is the valgrind output for the modified code that is modified using for loop in memser()
Code :
==20955== Memcheck, a memory error detector
==20955== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==20955== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==20955== Command: ./a.out
==20955== Parent PID: 20744
==20955==
==20955== Invalid read of size 8
==20955== at 0x3A05862397: getdelim (in /lib64/libc-2.5.so)
==20955== by 0x4014B1: read_text_file (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x400E4B: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== Address 0x5ef9b70 is 0 bytes after a block of size 6,556,464 alloc'd
==20955== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955== by 0x400BAD: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Invalid write of size 8
==20955== at 0x3A058624D6: getdelim (in /lib64/libc-2.5.so)
==20955== by 0x4014B1: read_text_file (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x400E4B: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== Address 0x5ef9b70 is 0 bytes after a block of size 6,556,464 alloc'd
==20955== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955== by 0x400BAD: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Conditional jump or move depends on uninitialised value(s)
==20955== at 0x4A0759A: strcat (mc_replace_strmem.c:174)
==20955== by 0x40160E: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Invalid write of size 1
==20955== at 0x4A075B7: strcat (mc_replace_strmem.c:174)
==20955== by 0x40161C: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20955== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Invalid write of size 1
==20955== at 0x4A075C9: strcat (mc_replace_strmem.c:174)
==20955== by 0x40161C: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== Address 0x54d83c5 is 1 bytes after a block of size 4 alloc'd
==20955== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Invalid read of size 1
==20955== at 0x4A075BD: strcat (mc_replace_strmem.c:174)
==20955== by 0x401629: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x400F32: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20955== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Conditional jump or move depends on uninitialised value(s)
==20955== at 0x40114C: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Invalid write of size 1
==20955== at 0x4A075B7: strcat (mc_replace_strmem.c:174)
==20955== by 0x40161C: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x401198: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20955== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Invalid write of size 1
==20955== at 0x4A075C9: strcat (mc_replace_strmem.c:174)
==20955== by 0x40161C: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x401198: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== Address 0x54d83c5 is 1 bytes after a block of size 4 alloc'd
==20955== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955== Invalid read of size 1
==20955== at 0x4A075BD: strcat (mc_replace_strmem.c:174)
==20955== by 0x401629: file_name_generator (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== by 0x401198: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955== Address 0x54d83c4 is 0 bytes after a block of size 4 alloc'd
==20955== at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==20955== by 0x400AE8: main (in /misc/data_seis06/msjameel/medical/cleaned_files/word_cohesion/a.out)
==20955==
==20955==
==20955== More than 10000000 total errors detected. I'm not reporting any more.
==20955== Final error counts will be inaccurate. Go fix your program!
==20955== Rerun with --error-limit=no to disable this cutoff. Note
==20955== that errors may occur in your program without prior warning from
==20955== Valgrind, because errors are no longer being displayed.
==20955==
==20955==
==20955== HEAP SUMMARY:
==20955== in use at exit: 128,109,778 bytes in 3,047,279 blocks
==20955== total heap usage: 3,047,508 allocs, 229 frees, 147,908,818 bytes allocated
==20955==
==20955== LEAK SUMMARY:
==20955== definitely lost: 13,800 bytes in 115 blocks
==20955== indirectly lost: 0 bytes in 0 blocks
==20955== possibly lost: 0 bytes in 0 blocks
==20955== still reachable: 128,095,978 bytes in 3,047,164 blocks
==20955== suppressed: 0 bytes in 0 blocks
==20955== Rerun with --leak-check=full to see details of leaked memory
==20955==
==20955== For counts of detected and suppressed errors, rerun with: -v
==20955== Use --track-origins=yes to see where uninitialised values come from
==20955== ERROR SUMMARY: 10000000 errors from 10 contexts (suppressed: 4 from 4)