未定义的行为? [英] undefined behaviour?
问题描述
我偶然发现了以下代码(剥离到最低限度)
#include< stdio.h>
#include< stdlib.h>
int main(无效){
char * file_name =" t1.txt" ;;
char ch;
FILE * pf = fopen(file_name," r +");
fputs(" bla",pf);
ungetc (''z'',pf);
ch = getc(pf);
printf(" ch =%c\ n",ch);
fclose(pf);
返回0;
}
现在我想知道如果这是定义的行为与否,如果它定义为
,那么最后关闭是否会出现问题?
提前谢谢
弗里德里希
-
请删除just-for-news-通过电子邮件回复。
Friedrich Dominicus写道:我偶然发现了以下代码(剥离到最低限度)
#include< stdio.h>
#include< stdlib.h>
int main(void){
char * file_name =" t1.txt英寸;
这里使用了一个未初始化的指针。
char ch;
FILE * pf = fopen(file_name," r +");不检查
fopen()是否有错误。
fputs(" bla",pf);
再次,fputs()不会检查失败。
ungetc(''z'',pf);
ungetc()的第一个参数是int。
再次,没有检查失败。
ch = getc(pf);
getc()返回一个int,但也可能在出错时返回EOF。
printf(" ch =%c\ nn,ch);
fclose(pf );
fclose()没有被检查错误。
返回0;
}
现在我想知道这是否已定义行为与否,如果定义了最终结束时是否存在问题?
AFAICS,fclose()应该没有问题。
这个程序的问题是在开始时使用未初始化的
指针。
我在这里测试了它它崩溃了。这是因为未初始化的
指针。
使用字符串文字或通过malloc分配空间或使用
数组
保存文件名。
2005年12月15日星期四14:19:26 +0100,Friedrich Dominicus写道:
#include< stdio.h>
#include< stdlib.h>
int main(void){
char * file_name =" t1.txt";
char ch;
FILE * pf = fopen(file_name," r +");
fputs(" bla",pf );
如果fopen()失败,肯定是未定义的行为。除此之外,没有。
ungetc(''z'',pf);
一个ungetc()保证可以正常工作。所以,没问题。
ch = getc(pf);
getc()返回一个int。如果
getc()失败并且char已签名,则可能会出现实现定义的行为。
这是因为getc()如果失败则返回EOF,可能不适合
a char。
如果字符未签名,则值为EOF,并加上CHAR_MAX + 1
直到值可以用字符表示。
如果字符已签名,并且EOF无法在其中表示,则生成的
值是实现定义的。部分行为可能是一个提升的
信号。
但是,我不知道如果你刚刚打电话,是否允许getc()失败
ungetc()。我不认为标准对此有任何保证。
printf(" ch =%c\ nn,ch);
printf()期望将unsigned char转换为int。由于默认的整数提升规则,转换为
int是自动完成的,
但不保证char不被签名。
我相信这是未定义的行为。
fclose(pf);
没问题。
返回0;
}
- -
Pieter Droogendijk< pieter at binky dot org dot uk>
PGP / 1E92DBBC [为Emperor's Finest让路。 ] binky.org.uk
2005-12-15,Friedrich Dominicus< ju ************* ****@q-software-solutions.de>写道:我偶然发现了以下代码(剥离到最低限度)
#include< stdio.h>
#include< stdlib.h>
int main(void){
char * file_name =" t1.txt" ;;
char ch;
FILE * pf = fopen(file_name," r +" );
是否可以打开文本文件进行更新
实现定义。也就是说,这可能是r + b。没有
改变你问题的基本含义。
fputs(" bla",pf);
ungetc(''z'',pf);
ch = getc(pf);
printf(" ch =%c\ n,ch);
fclose(pf);
返回0;
}
现在我想知道这是否是已定义的行为,如果它被定义,那么最后关闭是否会出现问题?
我不能想到那里的一行调用未定义的
行为......但是,我不清楚之后是否明确定义了
档案的内容。
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
fputs("bla", pf);
ungetc(''z'', pf);
ch = getc(pf);
printf("ch = %c\n", ch);
fclose(pf);
return 0;
}
Now I''m wondering if this is defined behaviour or not, and if it''s
defined should there be a problem with the close at the end?
Thanks in advance
Friedrich
--
Please remove just-for-news- to reply via e-mail.
Friedrich Dominicus wrote:I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *file_name = "t1.txt"; An uninitialised pointer is being used here.
char ch;
FILE *pf = fopen(file_name, "r+"); fopen() is not checked for errors.
fputs("bla", pf); Again, fputs() is not checked for failure.
ungetc(''z'', pf); The first parameter of ungetc() is an int.
Again, no checking for failure.
ch = getc(pf); getc() returns an int, but may also return EOF on error.
printf("ch = %c\n", ch);
fclose(pf); fclose() isn''t being checked for error.
return 0;
}
Now I''m wondering if this is defined behaviour or not, and if it''s
defined should there be a problem with the close at the end?
AFAICS, there should be no problems with fclose().
The problem in this program is with the use of a uninitialised
pointer at the start.
I tested it here and it crashes. It because of the uninitialised
pointer.
Either use a string literal or allocate space via malloc or use an
array
to hold the file name.
On Thu, 15 Dec 2005 14:19:26 +0100, Friedrich Dominicus wrote:
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
fputs("bla", pf);
Definitely undefined behaviour if fopen() failed. Other than that, no.
ungetc(''z'', pf);
One ungetc() is guaranteed to work. So, no problem.
ch = getc(pf);
getc() returns an int. Implementation-defined behaviour might arise if
getc() fails and if a char is signed.
This because getc() returns EOF if it fails, which might not fit in
a char.
If chars are unsigned, the value will be EOF with CHAR_MAX+1 added to it
until the value can be represented in a char.
If chars are signed, and EOF cannot be represented in it, the resulting
value is implementation-defined. Part of the behaviour might be a raised
signal.
However, I don''t know if getc() is allowed to fail if you just called
ungetc(). I don''t think the standard makes any guarantees about this.
printf("ch = %c\n", ch);
printf() expects an unsigned char converted to an int. The conversion to
int is done automatically thanks to the default integer promotion rules,
but char is not guaranteed to be unsigned.
I believe this is undefined behaviour.
fclose(pf);
No problem with this.
return 0;
}
--
Pieter Droogendijk <pieter at binky dot org dot uk>
PGP/1E92DBBC [ Make way for the Emperor''s Finest. ] binky.org.uk
On 2005-12-15, Friedrich Dominicus <ju*****************@q-software-solutions.de> wrote:I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
Whether it is possible to open a text file for updating is
implementation-defined. that said, this could have been "r+b" without
changing the essential meaning of your question.
fputs("bla", pf);
ungetc(''z'', pf);
ch = getc(pf);
printf("ch = %c\n", ch);
fclose(pf);
return 0;
}
Now I''m wondering if this is defined behaviour or not, and if it''s
defined should there be a problem with the close at the end?
I can''t think of a single line in there that invokes undefined
behavior... however, it''s not clear to me whether the contents of the
file are well-defined afterwards.
这篇关于未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!