读取文件时,我通常会检查
read
是否返回负数 iostat
以查看是否到达文件末尾。
如果使用 intel 编译器(2022 或 2023 版本的 ifort 和 ifx),Valgrind 会抱怨
Conditional jump or move depends on uninitialised value(s)
。一个最小的例子是:
program test_reading
implicit none
integer :: err_flag
character(len = 5) :: input
do
read (*, *, iostat = err_flag) input
if (err_flag < 0) exit ! EOF
end do
end program
对于任何任意输入(这里我用字母“e”测试它),我从 Valgrind 收到以下警告:
$ valgrind ./a.out
==630192== Memcheck, a memory error detector
==630192== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==630192== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==630192== Command: ./a.out
==630192==
e
==630192== Conditional jump or move depends on uninitialised value(s)
==630192== at 0x41C8FD: for__characterize_LUB_buffer (in /path/to/a.out)
==630192== by 0x41CB70: for__get_s (in /path/to/a.out)
==630192== by 0x409495: for_read_seq_lis (in /path/to/a.out)
==630192== by 0x40422D: MAIN__ (test2.f90:6)
==630192== by 0x40419C: main (in /path/to/a.out)
==630192==
为什么会这样?在没有 Valgrind 任何警告的情况下读入文件直至其结束的推荐方法是什么?
当我使用 gfortran 时,我没有收到此警告。优化级别没有影响。
带上线就可以了
if (err_flag < 0) exit ! EOF
之前
read (*, *, iostat = err_flag) input