在C异常处理 - 什么是()返回0使用的setjmp? [英] Exception Handling in C - What is the use of setjmp() returning 0?

查看:95
本文介绍了在C异常处理 - 什么是()返回0使用的setjmp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个问题涉及对setjmp / longjmp的用法 -

I have a few questions relating to setjmp/longjmp usage -


  1. 什么是返回0,这是默认的,我们可以不影响使用的setjmp(JMP ___ BUF stackVariables)。

  1. What is the use of setjmp(jmp___buf stackVariables) returning 0. It is a default, which we cannot influence.

时的setjmp的唯一意义(stackVariables)是推栈stackVariables。基本上0告诉我们,如果堆栈于stack_variables推成功。

Is the only significance of setjmp(stackVariables) is to push the stack in stackVariables. And basically 0 tells us if the stack was pushed on stack_variables successfully.

他们是当值一次非零(非零)当您从longjmp的返回。什么是从lomgjmp回来,当你从longjmp的,当你的异常处理返回。这是设置是让人有些困惑。

Their is one occasion when the value is non-zero (any non-zero) when you return from a longjmp. What is returning from a lomgjmp, when do you return from longjmp, when your exception is handled. This is setup is really confusing.

能否请一些有关它试图/抛出和捕获。而将是非常巨大的,如果能够提供的setjmp / longjmp的一些很好的例子。

Can some please relate it to try/throw and catch. And would be really great, if some good examples of setjmp/longjmp could be provided.

时的longjmp像扔,它只是例外的地方可以提高的地方后调用。

Is longJmp like throw, and it is called just after the place where exception can be raised.

感谢。

推荐答案

的C99规范给出了:

如果返回的是从直接调用,那么setjmp宏返回值为零。如果
  回报是来自于longjmp函数的调用,那么setjmp的宏返回一个非零
  值。

If the return is from a direct invocation, the setjmp macro returns the value zero. If the return is from a call to the longjmp function, the setjmp macro returns a nonzero value.

因此​​,答案为1是一个零表示你已经叫的setjmp 第一次和非零表示它是从一个返回 longjmp的

So the answer to 1 is that a zero indicates you have called setjmp the first time, and non-zero indicates it is returning from a longjmp.


  1. 这将当前的程序状态。一个longjmp的之后,状态被恢复,则控制返回到该点它被称为,并且返回值是非零

  1. It pushes the current program state. After a longjmp, the state is restored, control returns to the point it was called, and the return value is non-zero.

有C语言也不例外它的排序类似于根据返回无论你在原工艺,或第二工艺的不同值它继承了环境,如果你熟悉了。

There are no exceptions in C. It's sort-of similar to fork returning different values depending whether you're in the original process, or a second process which has inherited the environment, if you're familiar with that.

尝试 / 在C ++中会调用就扔了与追赶之间的所有自动对象的析构函数。 的setjmp / 的longjmp 将不会调用析构函数,因为它们不存在C.所以,你是你自己的据调用免费在任何你所的malloc ED的平均时间。

try/catch in C++ will call destructors on all automatic objects between the throw and the catch. setjmp/longjmp will not call destructors, as they don't exist in C. So you are on your own as far as calling free on anything you've malloced in the mean time.

使用的条件,这样的:

#include <stdio.h>
#include <setjmp.h>
#include <string.h>
#include <stdlib.h>

void foo ( char** data ) ;
void handle ( char* data ) ;
jmp_buf env;

int main ()
{
    char* data = 0;

    int res = setjmp ( env ); 
    // stored for demo purposes. 
    // in portable code do not store 
    // the result, but test it directly.

    printf ( "setjmp returned %d\n", res );

    if ( res == 0 )
        foo ( &data );
    else
        handle ( data );

    return 0;
}


void foo ( char** data )
{
    *data = malloc ( 32 );

    printf ( "in foo\n" );

    strcpy ( *data, "Hello World" );

    printf ( "data = %s\n", *data );

    longjmp ( env, 42 );
}

void handle ( char* data )
{
    printf ( "in handler\n" );

    if ( data ) {
        free ( data );
        printf ( "data freed\n" );
    }
}

大致相当于

#include <iostream>

void foo ( ) ;
void handle ( ) ;

int main ()
{
    try {
        foo ();
    } catch (int x) {
        std::cout << "caught " << x << "\n";
        handle ();
    }

    return 0;
}

void foo ( )
{
    printf ( "in foo\n" );

    std::string data = "Hello World";

    std::cout << "data = " << data << "\n";

    throw 42;
}

void handle ( )
{
    std::cout << "in handler\n";
}

在C的情况下,你必须做明确的内存管理(虽然通常你会释放它在与调用longjmp前malloc分配它的功能,因为它使生活更简单)

In the C case, you have to do explicit memory management (though normally you'd free it in the function which malloc'd it before calling longjmp as it makes life simpler)

这篇关于在C异常处理 - 什么是()返回0使用的setjmp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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