gpt4 book ai didi

c - Valgrind : Invalid read of size 1

转载 作者:行者123 更新时间:2023-12-05 09:30:53 24 4
gpt4 key购买 nike

我有以下代码,但我无法弄清楚为什么我会从 valgrind 收到此错误(对于上下文,我试图将文本从文件复制到字符串变量中)

int main(int argc, char **argv)
{
FILE *fp;
int f_size;
char *string;

fp = fopen("./src/input.txt", "rw+");
if (fp == NULL)
{
printf("Error!");
exit(1);
}

fseek(fp, 0, SEEK_END);
f_size = ftell(fp);
fseek(fp, 0, SEEK_SET);

string = (char *)malloc(sizeof(char) * f_size);
fread(string, sizeof(char), f_size, fp);
printf("\n%s\n", string);

free(string);
fclose(fp);
return 0;
} /* main */

我通过 printf 得到以下错误

==19085== Memcheck, a memory error detector
==19085== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19085== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==19085== Command: ./build/find_word
==19085==

==19085== Invalid read of size 1
==19085== at 0x483EF54: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==19085== by 0x48CEE94: __vfprintf_internal (vfprintf-internal.c:1688)
==19085== by 0x48B7EBE: printf (printf.c:33)
==19085== by 0x10958C: main (main.c:44)
==19085== Address 0x4a4932e is 0 bytes after a block of size 142 alloc'd
==19085== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==19085== by 0x109555: main (main.c:42)
==19085==
text from file
==19085==
==19085== HEAP SUMMARY:
==19085== in use at exit: 0 bytes in 0 blocks
==19085== total heap usage: 4 allocs, 4 frees, 5,734 bytes allocated
==19085==
==19085== All heap blocks were freed -- no leaks are possible
==19085==
==19085== For lists of detected and suppressed errors, rerun with: -s
==19085== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

最佳答案

您分配的缓冲区足够大,可以容纳从文件中读取的字符,但不能容纳其他任何东西。假设文件只包含文本,这意味着 string 实际上不是字符串,因为没有终止空字节。因此,当您将它传递给 printf 时,它会读取已分配内存的末尾,这就是 valgrind 告诉您的内容。

您需要将分配的内存量加 1,并在填充缓冲区后手动将 0 写入最后一个字节以获得正确的字符串。

string = malloc(sizeof(char) * f_size + 1);
fread(string, sizeof(char), f_size, fp);
string[f_size] = 0;

关于c - Valgrind : Invalid read of size 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69255005/

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