尝试运行优化的C文件时出现fgets警告 [英] Getting fgets warning when trying to run optimized c file
问题描述
这是一个大得多的文件的一部分,但这是唯一有问题的功能.这就是问题所在,如果我在未经优化的gcc中编译它,我会得到我想要的一切而不会出现问题.但是,如果我尝试将其编译为gcc -c -pg -02 main.c,我收到以下错误消息
This is part of a much larger file, but this is the only function in question. Here's the issue, if I compile it in gcc unoptimized, I will get everything I want without issues. However, if I try to compile it as gcc -c -pg -02 main.c, I get the following error message
推荐答案
像其他任何输入函数一样, fgets
可能由于各种原因而失败.您可以从其文档中找到,如果发生这种情况,它将返回 NULL
.但是,由于您不查看其返回值,因此您永远不会知道.这就是编译器试图警告您的内容.如果失败,则数组 line
将不包含有效的输入,并且可能包含垃圾,如果您尝试对其进行处理,它将导致程序行为异常.
Like any other function that does input, fgets
could fail for a variety of reasons. As you can find from its documentation, if this happens it will return NULL
. However since you do not look at its return value, you would never know. That's what the compiler is trying to warn you about. If it fails, the array line
will not contain valid input, and may contain garbage that would cause your program to misbehave if you try to process it.
(在许多情况下,只有在优化处于启用状态时才会出现一些警告;这是因为作为优化过程的一部分,编译器会对代码进行更详细的分析,这使它更有可能检测到此类问题.但是这是一件好事;这不是优化的问题,也不是不使用它的理由.)
(There are many situations where some warnings only happen with optimization on; this is because the compiler does a more detailed analysis of the code as part of the optimization process, which makes it more possible for it to detect such issues. But that's a good thing; it's not a problem with optimization or a reason not to use it.)
如果 fgets
应该失败,则您的程序没有明显的恢复方法,因此最简单的方法是退出并显示错误消息. perror
函数是一种方便的方法;它从 frnos
应该设置的 errno
变量中打印出一条与错误代码相对应的可读消息.
If fgets
should fail, there's no obvious way for your program to recover, so the simplest approach is to just have it quit and display an error message. The perror
function is a convenient way to do that; it prints a human-readable message corresponding to the error code from the errno
variable which fgets
should set.
因此,这里错误检查的基本形式是将您的 fgets
行替换为:
So a rudimentary form of error checking here would be to replace your fgets
line with:
if (fgets(line,MAX_LINE ,stdin) == NULL) {
perror("Failed to read input");
exit(1);
}
一些您以后可以改进的地方:
Some things you could improve later:
-
fgets
返回NULL的一种可能原因是文件结尾:如果根本没有输入:用户在其终端上按下文件结尾键(通常在Unix上为Ctrl-D,在Windows上为Ctrl-Z)或从空文件重定向输入.这不完全是错误,并且不会在errno
中导致错误代码,因此在这种情况下,perror
可能会打印出误导性消息.尝试修复此错误.您可以使用ferror
函数区分这两种情况.
One possible reason for
fgets
to return NULL is end-of-file: if there was no input at all: the user hit the end-of-file key on their terminal (usually Ctrl-D on Unix, Ctrl-Z on Windows) or redirected input from an empty file. That's not exactly an error, and it won't result in an error code inerrno
, soperror
may print a misleading message in this case. Try and fix this bug. You could distinguish the two cases using theferror
function.
如果成功读取了一行但无法将其解析为数字,则 strtol
将失败.您目前也没有检查.查找它如何指示此故障,并相应地进行处理.
If a line was successfully read but can't be parsed as a number, strtol
will fail. You currently don't check for that either. Look up how it indicates this failure and handle it accordingly.
这篇关于尝试运行优化的C文件时出现fgets警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!