Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use setjmp

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "use of setjmp", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the use of setjmp" bar!

Setjmp and longjmp for C language have always known such a function, but they don't know when to use it, or even what the implementation mechanism of setjmp looks like.

Although setjmp is not used to implement coroutine this time, because all operations of setjmp to implement coroutine can be done in user mode, the implementation mechanism of setjmp is studied by the way.

As I guess, on call stack, setjmp must precede or equal to the position of longjmp, so all longjmp does is restore the current register context to the value saved by the setjmp function. The function calling longjmp shares a stack space with the function calling setjmp, so the result of longjmp recovering the register is to restore the register context and free the stack space to the place where the setjmp is located.

Then this explains why the setjmp usage function cannot be encapsulated in another layer, assuming that the setjmp code is encapsulated as follows:

Int try (jmp_buf jmp)

{

Int err

Err = setjmp (jmp)

Printf ("% d\ n", err)

Return err

}

Void execption (jmp_buff j, int err)

{

Return longjmp (j, err)

}

Int main ()

{

Jmp_buff j

Try (j)

Execption (j, 2)

}

As in the above code, the try function actually shares part of the stack space with execption. When longjmp returns to the try function, even if the register is restored, in fact, the stack structure of the try function has been destroyed, and the behavior of the try function after longjmp is unpredictable.

-

| | main |

|

| | try | excption |

|-| |

| |-|-|

When using setjmp, we must pay attention to the problem of whether the stack is overwritten, that is, the information saved by esp when calling setjmp should not have more free space than when setjmp saves esp before calling longjmp. If the stack increases from top to bottom, the value of esp between setjmp and longjmp must not be greater than the value of esp saved by setjmp.

Thank you for your reading, the above is the content of "the use of setjmp", after the study of this article, I believe you have a deeper understanding of the use of setjmp, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report