Will C++ exceptions safely propagate through C code?(C++ 异常会通过 C 代码安全地传播吗?)
问题描述
我有一个 C++ 应用程序,它调用 SQLite(SQLite 在 C 中)sqlite3_exec() 反过来又可以调用我在 C++ 中实现的回调函数.SQLite 被编译成静态库.
I have a C++ application that calls SQLite's (SQLite is in C) sqlite3_exec() which in turn can call my callback function implemented in C++. SQLite is compiled into a static library.
如果异常逃脱了我的回调,它会通过 SQLite 的 C 代码安全地传播到调用 sqlite3_exec() 的 C++ 代码吗?
If an exception escapes my callback will it propagate safely through the C code of SQLite to the C++ code calling sqlite3_exec()?
推荐答案
我猜这与编译器有关.但是,在回调中抛出异常将是一个非常糟糕的主意.要么它完全不起作用,要么 SQLite 库中的 C 代码将无法处理它.考虑这是否是 SQLite 中的一些代码:
My guess is that this is compiler dependent. However, throwing an exception in the callback would be a very bad idea. Either it will flat-out not work, or the C code in the SQLite library will be unable to handle it. Consider if this is some code in SQLite:
{
char * p = malloc( 1000 );
...
call_the_callback(); // might throw an exception
...
free( p );
}
如果异常有效",则 C 代码无法捕获它,并且 p 将永远不会被释放.当然,图书馆可能分配的任何其他资源也是如此.
If the exception "works", the C code has no possible way of catching it, and p will never be freed. The same goes for any other resources the library may have allocated, of course.
这篇关于C++ 异常会通过 C 代码安全地传播吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 异常会通过 C 代码安全地传播吗?


- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 近似搜索的工作原理 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01