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 代码安全地传播吗?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
