C / C ++:跳转到for循环 [英] C/C++: goto into the for loop

查看:153
本文介绍了C / C ++:跳转到for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点不寻常的情况 - 我想使用goto语句跳到的循环,而不是从中跳出

I have a bit unusual situation - I want to use goto statement to jump into the loop, not to jump out from it.

有充分的理由这样做 - 这code必须是一些功能,这使得第一次呼叫经过一番计算,以请求新的数据返回,需要多一个电话,继续的一部分。函数指针(明显的解决方案)不能使用,因为我们需要code不支持函数指针的互操作性。

There are strong reasons to do so - this code must be part of some function which makes some calculations after the first call, returns with request for new data and needs one more call to continue. Function pointers (obvious solution) can't be used because we need interoperability with code which does not support function pointers.

我想知道是否低于code是安全的,也就是说,它会被正确地被所有符合标准的C / C ++编译器编译(我们需要的C和C ++)。

I want to know whether code below is safe, i.e. it will be correctly compiled by all standard-compliant C/C++ compilers (we need both C and C++).

function foo(int not_a_first_call, int *data_to_request, ...other parameters... )
{
    if( not_a_first_call )
        goto request_handler;
    for(i=0; i<n; i++)
    {
        *data_to_request = i;
        return;
request_handler:
        ...process data...
    }
}

我研究的标准,但不是这样的用例的信息。我也想知道是否替换按等价的,而将但从便携性角度是有利的。

I've studied standards, but there isn't much information about such use case. I also wonder whether replacing for by equivalent while will be beneficial from the portability point of view.

先谢谢了。

UPD :感谢所有谁曾评论


  1. 所有的评论者:)是的,我明白,我不能在局部变量初始化跳,我必须保存/恢复 I 每个呼叫。

有关强的原因:)这code必须实现反向通信接口。反向通信是它试图避免使用函数指针的编码模式。有时候有,因为传统code要使用哪些期望您使用它。

about strong reasons :) This code must implement reverse communication interface. Reverse communication is a coding pattern which tries to avoid using function pointers. Sometimes it have to be used because of legacy code which expects that you will use it.

不幸的是,R-COMM接口不能在一个不错的方式实现。你不能使用函数指针,你不能轻易的工作分成几个功能。

Unfortunately, r-comm-interface can't be implemented in a nice way. You can't use function pointers and you can't easily split work into several functions.

推荐答案

似乎完全合法的。

从C99标准草案<一个href=\"http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm\">http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm在goto语句的部分:

From a draft of the C99 standard http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm in the section on the goto statement:

[#3] EXAMPLE 1 It is sometimes convenient to jump  into  the
   middle  of  a  complicated set of statements.  The following
   outline presents one possible approach to a problem based on
   these three assumptions:

     1.  The  general initialization code accesses objects only
         visible to the current function.

     2.  The  general  initialization  code  is  too  large  to
         warrant duplication.

     3.  The  code  to  determine  the next operation is at the
         head of the loop.  (To  allow  it  to  be  reached  by
         continue statements, for example.)

           /* ... */
           goto first_time;
           for (;;) {
                   // determine next operation
                   /* ... */
                   if (need to reinitialize) {
                           // reinitialize-only code
                           /* ... */
                   first_time:
                           // general initialization code
                           /* ... */
                           continue;
                   }
                   // handle other operations
                   /* ... */
           }

接下来,我们看一下for循环语句:

Next, we look at the for loop statement:

[#1]  Except for the behavior of a continue statement in the |
   loop body, the statement

           for ( clause-1 ; expr-2 ; expr-3 ) statement

   and the sequence of statements

           {
                   clause-1 ;
                   while ( expr-2 ) {
                           statement
                           expr-3 ;
                   }
           }

您的问题把两者结合起来告诉你,你是跳过去

Putting the two together with your problem tells you that you are jumping past

i=0;

成,而循环的中间。您将执行

into the middle of a while loop. You will execute

...process data...

然后

i++;

在控制流跳转到测试中,同时/ for循环

before flow of control jumps to the test in the while/for loop

i<n;

这篇关于C / C ++:跳转到for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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