我很难理解我的代码出了什么问题。我的程序有一个函数,一次从文件中读取 8 个字节的数据。
char *read_64_bit_data_from_file(FILE *file, size_t *number_of_chars_read ){
char *buffer = malloc(sizeof(char) * 8);
*number_of_chars_read = fread(buffer, 1, 8, file);
printf("%sn", buffer);
printf("Length of buffer: %ldn", strlen(buffer));
printf("Number of chars read: %ldn", *number_of_chars_read);
return buffer;
}
该函数的调用方式如下:
...
size_t num = 0;
size_t *number_of_chars_read = #
char *message = calloc(sizeof(char), 8);
do{
message = read_64_bit_data_from_file(file, number_of_chars_read);
}while(*number_of_chars_read==8);
...
现在,当我编译并运行程序时,我得到以下输出:
Two road�B�z
Length of buffer: 14
Number of chars read: 8
s divergx<�z
Length of buffer: 14
Number of chars read: 8
ed in a x<�z
Length of buffer: 14
Number of chars read: 8
yellow wx<�z
Length of buffer: 14
Number of chars read: 8
ood, Andx<�z
Length of buffer: 14
Number of chars read: 8
sorry I
Length of buffer: 8
Number of chars read: 8
could n
Length of buffer: 8
Number of chars read: 8`
问题是写入缓冲区的字符有时大于 8,尽管fread
每次都返回值 8。
现在,如果我使用calloc
而不是malloc
来分配buffer
,我会根据需要获得输出。以下是本例中的输出:
Two road
Length of buffer: 8
Number of chars read: 8
s diverg
Length of buffer: 8
Number of chars read: 8
ed in a
Length of buffer: 8
Number of chars read: 8
yellow w
Length of buffer: 8
Number of chars read: 8
ood, And
Length of buffer: 8
Number of chars read: 8
sorry I
Length of buffer: 8
Number of chars read: 8
could n
Length of buffer: 8
Number of chars read: 8`
我使用带有-Wall
和-Wextra
标志的GCC编译器编译了我的程序。 编译器未发出任何警告。
我通过valgrind
运行了该程序。以下是输出:
==6051== Invalid write of size 1
==6051== at 0x10917B: char_as_binary (in
/home/saksham/Documents/DES/DES)
==6051== by 0x1091FA: string_to_binary (in
/home/saksham/Documents/DES/DES)
==6051== by 0x108EE6: main (in /home/saksham/Documents/DES/DES)
==6051== Address 0x521ecf7 is 5 bytes after a block of size 2 alloc'd
==6051== at 0x4C31B25: calloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6051== by 0x1090EE: char_as_binary (in
/home/saksham/Documents/DES/DES)
==6051== by 0x1091FA: string_to_binary (in
/home/saksham/Documents/DES/DES)
==6051== by 0x108EE6: main (in /home/saksham/Documents/DES/DES)
==6051==
==6051== Invalid read of size 1
==6051== at 0x10900A: initial_permutation (in
/home/saksham/Documents/DES/DES)
==6051== by 0x108FA9: encrypt (in /home/saksham/Documents/DES/DES)
==6051== by 0x108EFD: main (in /home/saksham/Documents/DES/DES)
==6051== Address 0x521ec60 is 0 bytes after a block of size 64
alloc'd
==6051== at 0x4C31B25: calloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6051== by 0x1091D3: string_to_binary (in
/home/saksham/Documents/DES/DES)
==6051== by 0x108EE6: main (in /home/saksham/Documents/DES/DES)
==6051==
==6051==
==6051== HEAP SUMMARY:
==6051== in use at exit: 2,076 bytes in 172 blocks
==6051== total heap usage: 186 allocs, 14 frees, 12,972 bytes
allocated
==6051==
==6051== LEAK SUMMARY:
==6051== definitely lost: 2,076 bytes in 172 blocks
==6051== indirectly lost: 0 bytes in 0 blocks
==6051== possibly lost: 0 bytes in 0 blocks
==6051== still reachable: 0 bytes in 0 blocks
==6051== suppressed: 0 bytes in 0 blocks
==6051== Rerun with --leak-check=full to see details of leaked memory
==6051==
==6051== For counts of detected and suppressed errors, rerun with: -v
==6051== ERROR SUMMARY: 93 errors from 2 contexts (suppressed: 0 from
0)
我真的不明白为什么 malloc 会这样表现,以及使用 calloc 如何修复它。我相信我的程序有UB(未定义的行为)。
请帮助我了解我在监督什么以及我哪里出错了。
为了使 C 字符串样式的函数工作(例如strlen
),您需要为 NUL 终止符保留一个buffer
字节。
您没有这样做,因此程序的行为在形式上是未定义的。你的编译器没有注意到这一点,但Valgrind是。它还警告您丢失free
。考虑分配 9 个字节,将buffer[8]
设置为显式0
,并确保不要尝试将超过 8 个字节读取到buffer
中。