Review

Any reasonable programming language requires some form of modularization . By this we mean separating the program into well-defined and smaller modules that mutually interact in a precise fashion. Toward this end, C/C++ provide the mechanism of functions. In general, a data structure known as an activation frame is used to store all relevant information for the function to execute. A function call can be implemented in various ways, depending on the strategy for allocation of the activation frame. If a static allocation is used (in Fortran, e.g.) then multiple activations of a function are impossible , but the function call overhead is significantly reduced, leading to faster execution. If a dynamic allocation is used, the activation frame can be allocated on either the system heap or the system stack. Allocation on the system stack is more generally used because it gives the stack a role as the "control" stack; in such cases, the top of the stack and the activation tree truly represent the flow of control. Since C/C++ features dynamic allocation on the system stack, recursion can be used.

During a function call, control is passed to the callee (the function being called) and returned back to the caller (the function doing the calling) upon termination of the callee. There are several ways that arguments can be passed to functions. The copy-restore was used by early Fortran compilers but seldom since. Call-by- name for C/C++ is seen in macro-expansion and inlining. Call- by-value is exclusively used by C and is the default for C++.

In call-by-value, the arguments are passed by value - that is, their copies are passed. Whatever the function does with the arguments has no effect on the originals ' values, thus providing an environment that is safe in terms of the "information hiding" principle. In call-by-reference, the arguments are passed by reference (or by address or location), which really means passing a pointer to the argument. This facilitates direct access to the argument and thus allows the function to modify whatever is passed to it in this fashion. In C++, passing by reference of selected arguments is allowed (by explicit statement).

The activation frame also serves as the storage for all local objects of the function (that are not declared as static). Since the activation frame is "erased" after the function terminates, the values of local auto objects cannot be held between different activations.

Allowing multiple activations and hence recursion slows the execution, but the price is well worth it. Many problems have a convenient and relatively simple solution when recursion is used.



Memory as a Programming Concept in C and C++
Memory as a Programming Concept in C and C++
ISBN: 0521520436
EAN: 2147483647
Year: 2003
Pages: 64

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net