gpt4 book ai didi

c - 大小 8 的无效读取,大小 8 的无效写入,C

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:36 27 4
gpt4 key购买 nike

我有一个 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;
}

最佳答案

您正在为 data.gl_pathc 指针 + 1 个字节分配空间,然后您正在使用 data.gl_pathc + 1 指针(最后一个指针由 filenames[i] = NULL;

换句话说,你的分配

filenames = malloc(sizeof(char*) * (data.gl_pathc) + 1);

应该改为分配 data.gl_pathc + 1 指针;

filenames = malloc(sizeof(char*) * (data.gl_pathc + 1));

关于c - 大小 8 的无效读取,大小 8 的无效写入,C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18815089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com