February 22, 2002

Boolean Conversion

Numeric values and pointers are implicitly converted to a Boolean value in conditional expressions. For example: void *p = get_ptr();if(p){ // process p} A zero value evaluates as false. All other values evaluate as true. Note that negative values also evaluate as true.

Using Pointers as Integers

In the early days of C, pointers were plain integers. Relics of this approach can still be found in vintage APIs and legacy code. For instance, the ANSI C header declares the following typedef name: typedef void(*handler)(int signum); handler is a pointer to a function that takes int and returns

Notes about the system() Function

The system() function (declared in ) launches another program from the current program. As opposed to what most users think, it doesn’t return the exit code of the launched process. Instead, it returns the exit code of the shell that launches the process in question. Consider the following example: int

Returning Large Objects from a Function Efficiently and Safely

“getter” member functions usually return a result by value. However, when the returned value is a large object, a string for example, returning it by value can be very inefficient. Returning the result by reference rather than by value, as in: string & Person::get_name(){ return name;} solves the overhead problem.

A Recommended Exception Handling Policy

Exceptions report abnormal runtime situations. Although you have other mechanisms for detecting and reporting runtime errors, exceptions are advantageous because they automate the process of passing the error from a deep down infrastructure function to the highest function in the call chain.In general, infrastructure functions such as memory management routines

Uses of the

The volatile specifier disables various optimizations that a compiler might automatically apply and thus introduce bugs. I