这是错误还是未定义的行为? [英] Is this an error or undefined behaviour?

查看:101
本文介绍了这是错误还是未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序,当使用gcc和'' - std = c99'编译时,gcc说


test.c:6:错误:跳转到标识符范围内

可变修改类型


即它甚至不编译。


lcc-win32,另一方面报告


警告test.c:7无法访问的代码


生成的程序在运行时崩溃,但我想这可能是

预计因为x可能未初始化(UB?)

我的问题是,以下程序无效或未定义

行为被调用?


----------------------------------------- ---------

#include< stdio.h>

void func1(int n)

{

转到标签;

int x [n];

标签:

x [0] = 42;

printf(" x [0] =%d \ n",x [0]);

}


int main (无效)

{

func1(5);


返回0;

}

-------------------------------------- ------------

the following program, when compiled with gcc and ''-std=c99'', gcc says

test.c:6: error: jump into scope of identifier with
variably modified type

that is, it does not even compile.

lcc-win32, on the other hand, reports

Warning test.c: 7 unreachable code

the resulting program crashes at runtime, but I guess that can be
expected because x is probably uninitialized (UB?)
My question is, is the below program invalid or is undefined
behaviour invoked?

--------------------------------------------------
#include <stdio.h>
void func1(int n)
{
goto label;
int x[n];
label:
x[0] = 42;
printf("x[0] = %d\n", x[0]);
}

int main(void)
{
func1(5);

return 0;
}
--------------------------------------------------

推荐答案

Franz Hose写道:

。 ...
Franz Hose wrote:
....

我的问题是,以下程序无效或未定义

行为被调用?


---------------------------------------------- ----

#include< stdio.h>


void func1(int n)

{

转到标签;

int x [n];

标签:

x [0] = 42;

printf(" x [0] =%d \ n",x [0]);

}
My question is, is the below program invalid or is undefined
behaviour invoked?

--------------------------------------------------
#include <stdio.h>
void func1(int n)
{
goto label;
int x[n];
label:
x[0] = 42;
printf("x[0] = %d\n", x[0]);
}



第6.8.6.1p1节:goto语句不得从具有可变修改类型的标识符的

范围之外跳转到该标识符的

范围内"这发生在约束内,因此必须使用

诊断消息。我会说标准没有b $ b指定如果你违反了这个约束会发生什么,所以它是由于省略任何明确的定义而导致的未定义行为

行为"。


然而,从遗漏中推导UB总是一个棘手的争论 - 它可能是b
标准中其他地方说的内容提供了似乎已被省略的

定义。

Section 6.8.6.1p1: "A goto statement shall not jump from outside the
scope of an identifier having a variably modified type to inside the
scope of that identifier." This occurs within a constraint, so a
diagnostic message is mandatory. I would say that the standard doesn''t
specify what happens if you do violate that constraint, so it is
undefined behavior "by the omission of any explicit definition of the
behavior".

However, deriving UB from omission is always a tricky argument - it''s
possible that something said elsewhere in the standard provides the
definition that seems to have been omitted.


On Sun,2007年10月28日02:08: 04 +0000,James Kuyper写道:
On Sun, 28 Oct 2007 02:08:04 +0000, James Kuyper wrote:

我会说标准没有

指定如果你违反了那个会发生什么约束,所以它是

未定义的行为通过省略

行为的任何明确定义。


但是从遗漏中推导出UB总是一个棘手的论点 - 它可能是标准中其他地方提供的东西可能是b $ b e $>
定义似乎已被省略。
I would say that the standard doesn''t
specify what happens if you do violate that constraint, so it is
undefined behavior "by the omission of any explicit definition of the
behavior".

However, deriving UB from omission is always a tricky argument - it''s
possible that something said elsewhere in the standard provides the
definition that seems to have been omitted.



如果违反约束,并且实现接受程序

无论如何,当你运行它时会发生什么不属于

标准。明确允许实现提供扩展

,这些扩展不会改变严格符合程序的行为。

定义约束违规行为的扩展不能改变

严格符合程序的行为,也不违反标准的其他部分。

If a constraint is violated, and an implementation accepts the program
anyway, what happens when you run it is not within the scope of the
standard. Implementations are explicitly allowed to provide extensions
that don''t alter the behaviour of strictly conforming programs.
Extensions that define the behaviour of constraint violations can''t alter
the behaviour of strictly conforming programs, nor do they violate other
parts of the standard.


Harald van D ?3k写道:
Harald van D?3k wrote:

On Sun,2007年10月28日02:08:04 +0000,James Kuyper写道:
On Sun, 28 Oct 2007 02:08:04 +0000, James Kuyper wrote:

>我会说标准没有指明如果你违反了这个约束会发生什么,所以它是未定义的行为,因为省略了任何明确的定义。
行为

然而,从遗漏中获取UB总是一个棘手的论点 - 它可能是标准中其他地方所说的内容提供了
定义这似乎已被省略。
>I would say that the standard doesn''t
specify what happens if you do violate that constraint, so it is
undefined behavior "by the omission of any explicit definition of the
behavior".

However, deriving UB from omission is always a tricky argument - it''s
possible that something said elsewhere in the standard provides the
definition that seems to have been omitted.



如果违反约束,并且实现接受程序

无论如何,当你运行它时会发生什么不在范围内

标准。


明确允许实现提供扩展

不改变严格符合程序的行为。

定义约束违规行为的扩展不能改变严格符合程序的行为,也不会违反其他行为

部分标准。


If a constraint is violated, and an implementation accepts the program
anyway, what happens when you run it is not within the scope of the
standard.

Implementations are explicitly allowed to provide extensions
that don''t alter the behaviour of strictly conforming programs.
Extensions that define the behaviour of constraint violations can''t alter
the behaviour of strictly conforming programs, nor do they violate other
parts of the standard.



该标准不仅适用于严格符合规定的程序。如果

程序由于未定义的行为而没有严格遵守,则

所有投注均已关闭。如果它包含语法错误或约束违反或超过实现限制,那么允许实现拒绝它的实现
。但是,需要一个接受代码

没有未定义行为的实现,以符合标准的所有适用要求的方式翻译和执行它b / b


即使程序没有严格遵守。


合规实现可以扩展C语言,但

仅当扩展由一个构造启用时,该构造呈现C标准未定义的程序的

行为。

The standard doesn''t apply just to strictly conforming programs. If the
program is not strictly conforming because of undefined behavior, then
all bets are off. If it contains a syntax error or a constraint
violation, or exceeds an implementation limit, then an implementation is
allowed to reject it. However, an implementation which accepts code that
doesn''t have undefined behavior is required to translate and execute it
in a way that conforms to all applicable requirements of the standard,
even if the program isn''t strictly conforming.

Conforming implementations are allowed to extend the C language, but
only if the extension is enabled by a construct which renders the
behavior of the program undefined by the C standard.


这篇关于这是错误还是未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆