链接到C时,使用C的setjmp和longjmp使用++库 [英] Use of setjmp and longjmp in C when linking to C++ libraries

查看:182
本文介绍了链接到C时,使用C的setjmp和longjmp使用++库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用setjmp和longjmp的在链接到在C语言实现库中的C程序++(但有一个C API)。

I would like to use setjmp and longjmp in a C program that links to a library that is implemented in C++ (but has a C API).

C ++的code确实做了动态内存分配和指针获得通过API通过,但只要code的C面管理这些(不透明)正确的对象,不应该有任何梅辛使用起来的时候longjmp的,对吧?

The C++ code does do dynamic memory allocation and pointers get passed through the API, but as long as the C side of the code manages those (opaque) objects correctly, there shouldn't be any messing up when using longjmp, right?

我知道这不是安全使用C ++中code这些功能,但它应该是在链接到C ++ code C code安全吗?

I know it's not safe to use these functions in C++ code, but should it be safe in C code that is linked to C++ code?

推荐答案

这是你调用C ++函数从C code不作setjmp和longjmp的更不安全比他们始终是事实。

The fact that you call C++ functions from your C code does not make setjmp and longjmp more unsafe than they always are.

重要的是,如果你的库分配资源,你必须在地方恢复code,以确保这些被释放的longjmp被调用后正常。虽然这可能是很容易为自己分配,它可能会根据您使用C界面的结构是很难或不可能的C ++库。

What's important is that if your library allocates resources you must have recovery code in place to ensure that those are released properly after longjmp is called. While this is likely easy for your own allocations, it may be hard or impossible for the C++ library depending on how the C interface you use is structured.

这篇关于链接到C时,使用C的setjmp和longjmp使用++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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