Red Hat ENTERPRISE LINUX 4 Manual page 143

Hide thumbs Also See for ENTERPRISE LINUX 4:
Table of Contents

Advertisement

Chapter 6. Extensions to the C Language Family
}
The nested function can access all the variables of the containing function that are visible at the point
of its definition. This is called lexical scoping. For example, here we show a nested function which
uses an inherited variable named
bar (int *array, int offset, int size)
{
int access (int *array, int index)
{ return array[index + offset]; }
int i;
/* ... */
for (i = 0; i
/* ... */ access (array, i) /* ... */
}
Nested function definitions are permitted within functions in the places where variable definitions are
allowed; that is, in any block, before the first statement in the block.
It is possible to call the nested function from outside the scope of its name by storing its address or
passing the address to another function:
hack (int *array, int size)
{
void store (int index, int value)
{ array[index] = value; }
intermediate (store, size);
}
Here, the function
intermediate
calls
, the arguments given to
store
only so long as the containing function (
If you try to call the nested function through its address after the containing function has exited, all
hell will break loose. If you try to call it after a containing scope level has exited, and if it refers to
some of the variables that are no longer in scope, you may be lucky, but it's not wise to take the risk.
If, however, the nested function does not refer to anything that has gone out of scope, you should be
safe.
GCC implements taking the address of a nested function using a technique called trampolines. A
paper describing them is available as
http://people.debian.org/~aaronl/Usenix88-lexic.pdf.
A nested function can jump to a label inherited from a containing function, provided the label was
explicitly declared in the containing function (Section 6.2 Locally Declared Labels). Such a jump
returns instantly to the containing function, exiting the nested function which did the
intermediate functions as well. Here is an example:
bar (int *array, int offset, int size)
{
__label__ failure;
:
offset
size; i++)
receives the address of
are used to store into
store
hack
as an argument. If
store
, in this example) does not exit.
intermediate
. But this technique works
array
goto
135
and any

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the ENTERPRISE LINUX 4 and is the answer not in the manual?

Questions and answers

Table of Contents