gooca在alloca的功能范围内有效吗? [英] Is a goto in alloca's function scope valid?

查看:149
本文介绍了gooca在alloca的功能范围内有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准禁止进入VLA存在的功能范围.

The C standard prohibits a goto into a function scope where a VLA exists.

在低级别上,VLA和对alloca函数的调用应具有相同的结果.

A VLA and the call to alloca function should have the same result on low level.

(我可能是错的,因为我只是C语言,不是低级程序员,但按照我的想象,它似乎很机智)

(I could be wrong, as I'm just a C, not a low level programmer, but in my imagin that appears to be witty)

那么下面的代码片段也会被不确定吗?

So will the following snippet be also undefined behaivng?

int main()
{
    char *p;

    goto label1;

    {
        p = _alloca(1);
label1:
        p = NULL;
    }
}

我当然不能引用p,但是行为如何?

Ofcourse I cant refference p, but whats about the behaviour?

推荐答案

实际上,规则6.8.6.1指出:

Actually, the rule 6.8.6.1 states:

  A goto statement is not allowed to jump past any declarations of objects 
  with variably modified types.

在您的代码中,不存在具有可变修改类型的对象. alloca不会声明一个对象(编译器必须照顾).因此,没有像alloca的作用域那样的东西,也没有按照规则6.8.6.1.定义未定义行为的理由.

In your code, there does not exist an object with variably modified types. alloca does not declare an object (that the compiler has to care of). Thus, there is nothing like a scope for alloca, and no reason for undefined behavior in the sense of rule 6.8.6.1.

编辑

稍微详细说明一下答案:在VLA情况下,行为的不确定性"是由于承诺声明对象在其范围内(在语言级别)已知".通常,声明为代码执行设置了上下文.不需要在运行时执行.但是,对于VLA而言,情况并非如此:此处,此诺言在运行时部分实现,这打破了C的静态声明方法.为避免可能导致动态键入系统的进一步冲突,规则6.8.6.1避免了此类冲突.

To elaborate the answer a bit: the "undefinedness" of the behavior in case of VLA is due to the promise of a declaration that an object is "known" within its scope (at language level). In general, a declaration sets a context for code execution. There is no need that it is executed at runtime. However, this is not true in case of VLA: here this promise is implemented partly at runtime, breaking C's static declaration approach. To avoid further conflicts that would lead to a dynamic typing system, rule 6.8.6.1 avoids such conflicts.

相反,在语言级别alloca仅仅是一个函数;它的调用不构成任何范围.如果调用它,则仅对其运行时行为作出保证.如果未调用它,则我们不会期望"函数中的任何内容.因此,它的纯粹存在不会引起任何冲突:两种情况(绕过或不绕过)都具有定义明确的语义.

In contrast, at language level alloca is simply a function; its call does not constitute any scope. It makes only a promise about its run-time behavior in case it is called. If it isn't called, we do not "expect" anything from a function. Thus, its pure existence does not raise any conflict: both cases (bypassing or non-bypassing) have a well defined semantic.

这篇关于gooca在alloca的功能范围内有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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