开发者

Any good use for nested function in C?

开发者 https://www.devze.com 2023-01-27 01:03 出处:网络
I read somewhere that nested functions are permissible in C (at least the GNU compiler allows it).Consider the following code:

I read somewhere that nested functions are permissible in C (at least the GNU compiler allows it). Consider the following code:

/* nestedfunc.c */
#include <stdlib.h> /* for atoi(3) */
#include <stdio.h>

int F (int q)
{
  int G (int r)
  {
    return (q + r);
  }
  return (G (5));
}

int main (int argc, const char* argv[])
{
  int q = 0;
  if (argc > 1)
  {
    q = atoi (argv[1]);
开发者_开发技巧  }

  printf ("%d\n", F (q));
  return 0;
}

Compiling and running:

gcc -o nestedfunc -O2 -s -Wall nestedfunc.c
me@mybox:~/college/c++/other stuff$ ./nestedfunc 8
13
me@mybox:~/college/c++/other stuff$

I've also read that some other programming languages support these. My question is this: What useful purpose does a nested function have? Thanks in advance.


Nested functions can access the outer function's locals. Somewhat like closures, you can take a pointer to a nested function and pass this pointer to other functions, and the nested function will have access to the current invocation's locals (bad things happen if this invocation has already returned). Because the C runtime system is not designed for this, a function pointer is generally just a pointer to the first instruction of the function and pointers to nested functions can only be done by writing some code on the stack and passing a pointer to that. This is a bad idea from a security perspective.

If you want to use nested functions, use a language with a runtime system with proper support for them. To achieve a similar result in C, put "the outer function's locals" in a context structure and pass this along manually.


Nested functions provide encapsulation through lexical scope.

In your example, G() can only be called by F() and by other functions defined within F().


In general, a nested function is usually a helper function, which is only used inside one other function. It's sort of like a static function (with file scope) but even more localised (only function scope).

Nested functions are not standard C, so there's little reason to use them in that language.


In other programming languages (like Python, Ruby for example) functions are first class objects. You have closures which are powerful abstraction concept. In python you can do this:

def curry(func):
    from inspect import getfullargspec
    args = getfullargspec(func)
    num_args = len(args[0])

    def new_func(list_args, *args):
        l = len(list_args) + len(args)
        nl = list_args + list(args)
        if l > num_args:
             raise TypeError("Too many arguments to function")
        elif l == num_args:
             return func(*nl)
        else:
             return lambda *new_args: new_func(nl, *new_args)

    return lambda *args: new_func([], *args)

That is curry decorator which takes a function and makes it curried.

0

精彩评论

暂无评论...
验证码 换一张
取 消