What are some “good” ways to use longjmp/setjmp for C error handling?

前端 未结 6 1064
南旧
南旧 2020-12-24 14:09

I have to use C for one project and I am thinking of using longjmp/setjmp for error handling as I think it will be much easier to handle error in one central pl

6条回答
  •  滥情空心
    2020-12-24 14:10

    If you are worried about resource cleanup, you have to seriously wonder whether longjmp() and setjmp() are a good idea.

    If you design your resource allocation system so that you can in fact clean up accurately, then it is OK - but that design tends to be tricky, and typically incomplete if, in fact, the standard libraries that your code uses themselves allocate resources that must be released. It requires extraordinary care, and because it is not wholly reliable, it is not suitable for long-running systems that might need to survive multiple uses of the setjmp()/longjmp() calls (they'll leak, expand, and eventually cause problems).

提交回复
热议问题