Everything2
Near Matches
Ignore Exact
Full Text
Everything2

nested function

created by xriso

(idea) by xriso (2.5 y) (print)   ?   (I like it!) Mon Oct 22 2001 at 4:00:27

Here is some rather ridiculous code to demonstrate what a nested, or local, function would look like in C (note that GCC supports this as an extension):
int foo(int fred)
{
    int quux, yoink;
    
    int bar(int barney)
    {
        quux=barney;
        return quux * 2;
    }

    yoink = bar(fred + 1);
    return (yoink * quux);
}

The bolded portion of code contains bar, which is our nested function example. bar can only be called from foo (not exactly - see below about function pointers), but it can access any local variable of foo, such as quux. Other than these differences, the nested function is basically like another function.

A nested function seems to be a simple thing to implement -- at first. Here are the important issues that come up, and some solutions:

  • How does bar know where quux is?
    1. It simply assumes that the immediately preceding stack frame is from foo. Note that this will fail in any other case (call from function pointer, recursive nested function, call from another nested function).
    2. Or, to allow for recursion, some simple state information could be passed on the stack. As for function pointers, read on...
  • You can't take the function pointer of a nested function! You'd need some state information to know where quux is, so that the pointer could be used regardless of where it is used, but a function pointer is too simple for that!
    1. Ah, but we now consult the info page describing the GNU C Extension for nested functions. A little thing called a trampoline is used. Hideous? Yes. Works? Yes, at least as long as foo doesn't exit. If it does, well, anything can happen.
  • That's disgusting. Well, as long as the inner function can't goto the containing one, it should still be somewhat readable.
    1. Sorry to disappoint you. GNU C allows the nested function to goto somewhere in the function containing it, even if it was called from a function pointer. All the intermediate function calls will simply go away. Hideous? YES!!! Works? yes. But for the love of God, never ever ever use this if you plan for another human to understand your code! Those GCC developers are seriously twisted. What's next? comefrom?

printable version
chaos

COME FROM Trampoline nasal demons function pointer
lexical scope recursion nest Barney
bar Fred foo quux
yoink call stack info goto
Debian GNU/Linux GCC function System function
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
What you are reading:
World War II
Zap Comix
Enron
Windows 1.x
The Beatles (The White Album)
Geta
Sylvia Plath
McDonnell Douglas Warranty Card
Sometimes I actually don't mind having the slowest modem in the world
How to tune a guitar
Flapper
The Shinto Kami of Japan
The New Revised Guide To Internet Funni
New Writeups
antigravpussy
One fly amongst many(person)
sam512
Moon Base Shackleton, 1978(fiction)
Pavlovna
toy boy(person)
XWiz
tear jerker(review)
Heitah
Anarchy is Order(idea)
jessicaj
July 26, 2008(dream)
Berek
ABBA(person)
devolution
k-hole(place)
Nadine_2
The Sound Of Madness(review)
SwimmingMonkey
Conversations with Fo Fo, the Loneliest dog in Purgatory(fiction)
locke baron
lynx(thing)
Simulacron3
Reality, Dimensions and the Natural Ontology(essay)
SubSane
Making Love to a 9-Foot Woman(person)
Ouzo
Thoughts(idea)
antigravpussy
I fall silent, listening. The breadcrumbs are talking about us(person)
Everything 2 is brought to you by the letter C and The Everything Development Company