Suppose I have a C library that I’m linking into a C++ program on Windows using Visual Studio. This C library is black box. If a fatal error occurs in this library (such as dereferencing a null), how will this runtime error be handled by the program/OS? I know in C++ that there are null reference exceptions, so you can probably handle such errors with a try/catch, but since this is a C library it won’t issue a throw, right? So what will happen? The program will terminate, but through what means if not a C++ exception?
Suppose I have a C library that I’m linking into a C++ program on
Share
Dereferencing a null pointer in native code will cause an “access violation” on Windows, or a “segmentation fault” on Unix/Linux. Different names for the same thing. It’s the CPU that detects the error, and it invokes a handler in the operating system, which terminates the process.
Dereferencing a null reference in a VM-based language (e.g. .NET or Java) will throw an exception that you can catch. That’s possible because the VM sits between the program and the CPU, and it can detect the null before trying to actually dereference it.
A C library is native code, so you’ll get an access violation. You’d get the same from a true C++ program, which is also compiled to native code. But if you’re using Managed C++ or C++/CLI, those are variants which compile to CIL and run on the .NET runtime, so you get a NullReferenceException in that case.