我有一个 valgrind 错误,我不知道如何摆脱它们:
==5685== Invalid read of size 8
==5685== at 0x4008A1: main (in /home/mazix/Desktop/tests/filenames)
==5685== Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685== by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685== by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685==
==5685== Invalid read of size 8
==5685== at 0x4008BB: main (in /home/mazix/Desktop/tests/filenames)
==5685== Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685== by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685== by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685==
==5685==
==5685== FILE DESCRIPTORS: 3 open at exit.
==5685== Open file descriptor 2: /dev/pts/1
==5685== <inherited from parent>
==5685==
==5685== Open file descriptor 1: /dev/pts/1
==5685== <inherited from parent>
==5685==
==5685== Open file descriptor 0: /dev/pts/1
==5685== <inherited from parent>
==5685==
==5685==
==5685== HEAP SUMMARY:
==5685== in use at exit: 0 bytes in 0 blocks
==5685== total heap usage: 231 allocs, 231 frees, 43,693 bytes allocated
==5685==
==5685== All heap blocks were freed -- no leaks are possible
==5685==
==5685== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2)
==5685==
==5685== 1 errors in context 1 of 3:
==5685== Invalid read of size 8
==5685== at 0x4008BB: main (in /home/mazix/Desktop/tests/filenames)
==5685== Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685== by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685== by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685==
==5685==
==5685== 1 errors in context 2 of 3:
==5685== Invalid read of size 8
==5685== at 0x4008A1: main (in /home/mazix/Desktop/tests/filenames)
==5685== Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685== by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685== by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685==
==5685==
==5685== 1 errors in context 3 of 3:
==5685== Invalid write of size 8
==5685== at 0x400806: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685== by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685== Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685== by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685== by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685==
--5685--
--5685-- used_suppression: 2 dl-hack3-cond-1
==5685==
==5685== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2)
和我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glob.h>
char **getFilenames()
{
char **filenames = NULL;
glob_t data;
unsigned int i;
switch( glob("./*.*", 0, NULL, &data ) )
{
case 0:
break;
case GLOB_NOSPACE:
printf( "Out of memory\n" );
break;
case GLOB_ABORTED:
printf( "Reading error\n" );
break;
case GLOB_NOMATCH:
printf( "No files found\n" );
break;
default:
break;
}
filenames = malloc(sizeof(char*)*(data.gl_pathc)+1);
for(i=0; i<data.gl_pathc; i++)
{
int len = strlen(data.gl_pathv[i]);
filenames[i] = malloc(sizeof(char*)*len);
strcpy(filenames[i], data.gl_pathv[i]);
}
filenames[i] = NULL;
globfree( &data );
return filenames;
}
int main( int argc, char *argv[] )
{
char **filenames = getFilenames();
unsigned int i = 0;
for(i=0; filenames[i] != NULL; i++)
{
printf("%s\n", filenames[i]);
free(filenames[i]);
}
free(filenames[i]);
free(filenames);
return 0;
}