本文介绍了如果程序的一部分表现出未定义的行为,它会影响程序的其余部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

假设一个程序员忘记初始化他的一个自动变量,他使用了它的值,从而调用了未定义的行为.

Suppose that a programmer forgot to initialize one of his automatic variables, and that he used its value, thereby invoking undefined behavior.

...
int i = 0, j;
...
printf("value of 'j': %d\n", j);
...
...
char buf[256];
fputs("Enter query:", stdout);
fgets(buf, sizeof(buf), stdin);
... //process input
... perform other tasks

程序员注意到屏幕上的乱码,意识到他的程序是错误的,但它并没有崩溃并继续运行.

The programmer noticed gibberish characters on the screen, and realized that his program is erroneous, but it did not crash and went on anyway.

假设在这一点之后,程序提示用户输入并预期处理它,显示结果,并执行所有独立于未初始化变量的其他任务,是否鼓励程序员停止使用该程序,修复错误,重新编译,然后运行?程序的其余部分会不一致吗?

Supposing after this point, the program prompts the user for input and is expected to process it, display results, and perform other tasks all of which are independent from the uninitialized variable, is the programmer encouraged to discontinue the use of the program, fix the error, recompile, and run? Will the remainder of the program be inconsistent?

推荐答案

一旦到达一个行为未定义的语句,那么整个程序的行为就是未定义的.

Once a statement with undefined behaviour is reached, then the behaviour of the entire program is undefined.

矛盾的是,在此之前运行的语句的行为也是未定义的.

Paradoxically, the behaviour of statements that have ran prior to that are undefined too.

这篇关于如果程序的一部分表现出未定义的行为,它会影响程序的其余部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:01