Will C++ exceptions safely propagate through C code?

前端 未结 5 758
傲寒
傲寒 2020-11-28 11:44

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 stati

5条回答
  •  情话喂你
    2020-11-28 12:34

    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 );
    }
    

    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.

提交回复
热议问题