www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 1267]

Summary

  • On many systemsUNIX, LINUX, Mac OS X or Windows systems in particularit is possible to redirect input to a program and output from a program. Input is redirected from the UNIX, LINUX, Mac OS X or Windows command lines using the redirect input symbol (<) or a pipe (|). Output is redirected from the UNIX, LINUX, Mac OS X or Windows command lines using the redirect output symbol (>) or the append output symbol (>>). The redirect output symbol simply stores the program output in a file, and the append output symbol appends the output to the end of a file.

  • The macros and definitions of the variable arguments header <cstdarg> provide the capabilities necessary to build functions with variable-length argument lists.

  • An ellipsis (...) in a function prototype indicates that the function receives a variable number of arguments.

  • Type va_list is suitable for holding information needed by macros va_start, va_arg and va_end. To access the arguments in a variable-length argument list, an object of type va_list must be declared.

  • Macro va_start is invoked before the arguments of a variable-length argument list can be accessed. The macro initializes the object declared with va_list for use by macros va_arg and va_end.

  • Macro va_arg expands to an expression of the value and type of the next argument in the variable-length argument list. Each invocation of va_arg modifies the va_list object so that the object points to the next argument in the list.

  • Macro va_end facilitates a normal return from a function whose variable argument list was referred to by the va_start macro.

  • On many systemsUNIX, LINUX, Mac OS X and Windows in particularit is possible to pass command-line arguments to main by including in main's parameter list the parameters int argc and char *argv[]. Parameter argc is the number of command-line arguments. Parameter argv is an array of char *'s containing the command-line arguments.

  • The definition of a function must be entirely contained in one fileit cannot span two or more files.

  • Global variables must be declared in each file in which they are used.

  • Function prototypes can be used to declare functions in other program files. (The extern specifier is not required in a function prototype.) This is accomplished by including the function prototype in each file in which the function is invoked, then compiling the files together.

  • The storage-class specifier static, when applied to a file scope variable or a function, prevents it from being used by any function that is not defined in the same file. This is referred to as internal linkage. Global variables and functions that are not preceded by static in their definitions have external linkagethey can be accessed in other files if those files contain proper declarations and/ or function prototypes.

  • The static specifier is commonly used with utility functions that are called only by functions in a particular file. If a function is not required outside a particular file, the principle of least privilege should be enforced by using static.

  • When building large programs from multiple source files, compiling the program becomes tedious if making small changes to one file means that the entire program must be recompiled. Many systems provide special utilities that recompile only the modified program file. On UNIX systems, the utility is called make. Utility make reads a file called Makefile that contains instructions for compiling and linking the program.

  • Function exit forces a program to terminate as if it had executed normally.


  • [Page 1268]
  • Function atexit registers a function in a program to be called upon normal termination of the programi.e., either when the program terminates by reaching the end of main, or when exit is invoked.

  • Function atexit takes a pointer to a function (e.g., a function name) as an argument. Functions called at program termination cannot have arguments and cannot return a value.

  • Function exit takes one argumentnormally the symbolic constant EXIT_SUCCESS or the symbolic constant EXIT_FAILURE. If exit is called with EXIT_SUCCESS, the implementation-defined value for successful termination is returned to the calling environment. If exit is called with EXIT_FAILURE, the implementation-defined value for unsuccessful termination is returned.

  • When function exit is invoked, any functions registered with atexit are invoked in the reverse order of their registration, all streams associated with the program are flushed and closed and control returns to the host environment.

  • The volatile qualifier is used to prevent optimizations of a variable, because it can be modified from outside the program's scope.

  • C++ provides integer and floating-point suffixes for specifying the types of integer and floating-point constants. The integer suffixes are u or U for an unsigned integer, l or L for a long integer and ul or UL for an unsigned long integer. If an integer constant is not suffixed, its type is determined by the first type capable of storing a value of that size (first int, then long int). The floating-point suffixes are f or F for a float and l or L for a long double. A floating-point constant that is not suffixed is of type double.

  • The signal-handling library provides the capability to register a function to trap unexpected events with function signal. Function signal receives two argumentsan integer signal number and a pointer to the signal-handling function.

  • Signals can also be generated with function raise and an integer argument.

  • The general-utilities library (cstdlib) provides functions calloc and realloc for dynamic memory allocation. These functions can be used to create dynamic arrays.

  • Function calloc receives two argumentsthe number of elements (nmemb) and the size of each element (size)and initializes the elements of the array to zero. The function returns a pointer to the allocated memory or if the memory is not allocated, the function returns a null pointer.

  • Function realloc changes the size of an object allocated by a previous call to malloc, calloc or realloc. The original object's contents are not modified, provided that the amount of memory allocated is larger than the amount allocated previously.

  • Function realloc takes two argumentsa pointer to the original object (ptr) and the new size of the object (size). If ptr is null, realloc works identically to malloc. If size is 0 and the pointer received is not null, the memory for the object is freed. Otherwise, if ptr is not null and size is greater than zero, realloc tries to allocate a new block of memory for the object. If the new space cannot be allocated, the object pointed to by ptr is unchanged. Function realloc returns either a pointer to the reallocated memory or a null pointer.

  • The result of the goto statement is a change in the program's flow of control. Program execution continues at the first statement after the label in the goto statement.

  • A label is an identifier followed by a colon. A label must appear in the same function as the goto statement that refers to it.

  • A union is a data type whose members share the same storage space. The members can be almost any type. The storage reserved for a union is large enough to store its largest member. In most cases, unions contain two or more data types. Only one member, and thus one data type, can be referenced at a time.

  • A union is declared in the same format as a structure.


  • [Page 1269]
  • A union can be initialized only with a value of the type of its first member or another object of the same union type.

  • C++ enables the programmer to provide linkage specifications to inform the compiler that a function was compiled on a C compiler and to prevent the name of the function from being encoded by the C++ compiler.

  • To inform the compiler that one or several functions have been compiled in C, write the function prototypes as follows:

         extern "C" function prototype  // single function
    
         extern "C"   // multiple functions
         {
           function prototypes
         }
    

    These declarations inform the compiler that the specified functions are not compiled in C++, so name encoding should not be performed on the functions listed in the linkage specification. These functions can then be linked properly with the program.

  • C++ environments normally include the standard C libraries and do not require the programmer to use linkage specifications for those functions.


Previous Page
Next Page