C++ 异常会安全地通过 C 代码传播吗?

Will C++ exceptions safely propagate through C code?(C++ 异常会安全地通过 C 代码传播吗?)
本文介绍了C++ 异常会安全地通过 C 代码传播吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用 SQLite 的 C++ 应用程序(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 代码传播吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)