Using standard C libraries in modules
Standard C libraries, i.e. functions declared in standard headers such as stdio.h and stdlib.h, can be used in Alore C libraries. There are, however, some additional issues that need to addressed and that require some additional effort from the programmer in order to avoid problems in Alore C modules.
Most of these issues have already been discussed in this document. This section contains an overview of these issues and links to additional information if it can be found somewhere else in this document.
Freeing resources allocated by standard C library functions
The Alore garbage collector cannot see or automatically free memory and resources allocated using basic C functions such as malloc or fopen. The following issues are related to this:
- Objects that contain references to resources allocated using malloc or another function should define a finalization method #f and set the external data size using ASetExternalDataSize.
- Resources allocated using ordinary C memory allocation functions are not automatically freed in the presence of direct exceptions. See section Direct exceptions and freeing resources for a more detailed discussion.
Using file operations
Basic C file operations such as printf, fopen and read can be used in Alore C libraries, but the following issues should be addressed:
- When writing to standard output using C functions, you must first flush the corresponding Alore stream io::StdOut. After having written data to stdout, you must flush the C stream, for example by using fflush. Otherwise output may not appear in the expected order.
- When reading from stdin using both the Alore io::StdIn stream and the C stdin stream, buffering may cause unexpected behavior, since both of the stream implementations have independent and separate buffering mechanisms.
Performing blocking or long-running operations
Long-running or blocking C calls, for example file input/output, need to be surrounded with AAllowBlocking and AEndBlocking macros.