首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::longjmp

Defined in header <csetjmp>

void longjmp( std::jmp_buf env, int status );

加载执行上下文。env之前的调用保存到setjmp此函数不返回。控件转移到宏的调用站点。setjmp建立env.那个setjmp然后返回作为status...

如果调用setjmp退出后,行为是未定义的%28,换句话说,只有长时间跳转的调用堆栈才允许%29。

不调用自动对象的析构函数。如果替换...std::longjmp带着throwsetjmp带着catch将为任何自动对象执行一个非平凡的析构函数,std::longjmp还没有定义。

参数

env

-

variable referring to the execution state of the program saved by std::setjmp

status

-

the value to return from setjmp. If it is equal to ​0​, 1 is used instead

返回值

%280%29

注记

longjmpC中用于处理函数无法有意义返回的意外错误条件的机制。C++一般使用异常处理为了这个目的。

二次

代码语言:javascript
复制
#include <iostream>
#include <csetjmp>
 
std::jmp_buf jump_buffer;
 
[[noreturn]] void a(int count) 
{
    std::cout << "a(" << count << ") called\n";
    std::longjmp(jump_buffer, count+1);  // setjump() will return count+1
}
 
int main()
{
    volatile int count = 0; // local variables must be volatile for setjmp
    if (setjmp(jump_buffer) != 9) {
        a(count++);  // This will cause setjmp() to exit
    }
}

二次

产出:

二次

代码语言:javascript
复制
a(0) called
a(1) called
a(2) called
a(3) called
a(4) called
a(5) called
a(6) called
a(7) called
a(8) called

二次

另见

setjmp

saves the context (function macro)

c关于Longjmp的文档

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券