explanation required

Discussion in 'C Programming' started by aarklon@gmail.com, Mar 14, 2006.

  1. Guest

    Hi all,

    In the article http://en.wikipedia.org/wiki/C_language

    it is written as follows

    C has the following important features:

    1) A simple core language, with important functionality such as math
    functions and file handling provided by sets of library routines
    instead

    2) Focus on the procedural programming paradigm, with facilities for
    programming in a structured style

    3) A type system which prevents many operations that are not
    meaningful

    4) Use of a preprocessor language, the C preprocessor, for tasks such
    as defining macros and including multiple source code files

    5) Low-level access to computer memory via the use of pointers

    6) A minimalistic set of keywords

    7) Parameters that are passed by value. Pass-by-reference semantics
    may be simulated by explicitly passing pointer values.

    8) Function pointers and static variables, which allow for a
    rudimentary form of closures and runtime polymorphism

    9) Lexical variable scope

    10) Records, or user-defined aggregate datatypes (structs) which
    allow related data to be combined and manipulated as a whole

    Now my question is on the 8th point

    1) Can any body give examples for run time polymorphism in C
    is it similar to virtual functions in c++

    2)what exactly is this closure???

    http://en.wikipedia.org/wiki/Closure_(computer_science)

    says

    A closure is a function created by a program at run time. This idea
    is written as a function that appears entirely within the body of
    another function. The nested, inner function may refer to local
    variables of the outer function. As the outer function executes,
    it creates a closure of the inner function. The closure consists of
    the function code and a reference to any variables in the outer
    function's scope that the closure needs.

    As far as my understanding goes nested functions are not possible in C
     
    , Mar 14, 2006
    #1
    1. Advertising

  2. Ben C Guest

    On 2006-03-14, <> wrote:
    > As far as my understanding goes nested functions are not possible in C


    You can do them in gcc (they're one of the GNU extensions), but they're
    not closures.
     
    Ben C, Mar 14, 2006
    #2
    1. Advertising

  3. On 2006-03-14, <> wrote:
    > 2)what exactly is this closure???
    >
    > http://en.wikipedia.org/wiki/Closure_(computer_science)
    >
    > says
    >
    > A closure is a function created by a program at run time. This idea
    > is written as a function that appears entirely within the body of
    > another function. The nested, inner function may refer to local
    > variables of the outer function. As the outer function executes,
    > it creates a closure of the inner function. The closure consists of
    > the function code and a reference to any variables in the outer
    > function's scope that the closure needs.
    >
    > As far as my understanding goes nested functions are not possible in C


    Wikipedia is lying to you, or possibly you've misquoted/misunderstood
    it.

    A closure is a reference to a function, combined with values for some
    or all of the function's arguments. The idea is that you can pass this
    thing around and it looks like an ordinary function; the values are
    hidden.

    You can't do this in C, at least not transparently, but the common
    idiom found in many libraries where you pass "void *data" along with a
    function pointer is the same idea.

    (What's the history of qsort in this context? I've often wanted to be
    able to pass stuff through it this way.)

    The reason nested functions appear in the picture is that if you have
    a nested function that uses automatic storage from its enclosing
    environment, some form of implicit closure is required to allow the
    nested function to find that environment at runtime.

    However, standard C does not support nested functions, and I
    understand that the longtime extension gcc had to allow them has been
    removed.

    --
    - David A. Holland
    (the above address works if unscrambled but isn't checked often)
     
    David Holland, Mar 14, 2006
    #3
  4. In article <>,
    <> wrote:
    > 1) Can any body give examples for run time polymorphism in C
    > is it similar to virtual functions in c++


    A structure whose fields are function pointers; the code
    calls indirectly upon the function whose pointer is in the structure
    slot appropriate for the task required at the time.

    > 2)what exactly is this closure???


    > http://en.wikipedia.org/wiki/Closure_(computer_science)


    > A closure is a function created by a program at run time. This idea
    > is written as a function that appears entirely within the body of
    > another function.


    Closures do not need to be entirely within a different function:
    global closures are entirely possible.

    > The nested, inner function may refer to local
    > variables of the outer function. As the outer function executes,
    > it creates a closure of the inner function. The closure consists of
    > the function code and a reference to any variables in the outer
    > function's scope that the closure needs.


    > As far as my understanding goes nested functions are not possible in C


    Create a structure to hold the -addresses- of variables in the
    "outer function". Pass the structure to the routine that one wants
    to be the "inner function". The inner function can then read or write
    values of the outer function's variables, by using the addresses
    from the structure. If any of the variables were "automatic" in
    the local "outer function" then the addresses stored in the structure
    become invalid when the outer function returns; you can solve this
    by restricting the passed addresses to be those of "static" variables
    or malloc'd data..


    In my opinion, the essence of a closure does not involve
    nested functions; rather, that it involves a function that carries
    its own state along with it, so that you can invoke the function
    and it has access to the state without you having to pass the state in.
    In C, you can do one copy of that, by having the function have static
    variables, but you cannot "duplicate" the code of an existing
    function but with different state. For example in one programming
    language I use, rand() does not itself generate random numbers: instead
    it returns a function which, when called, would generate random
    numbers within the limits you passed into rand; something like that
    requires creating a new function each time, which C cannot do.

    The difference between a closure and an object is that an object
    is understood to be a data entity which knows which functions are
    appropriate for it, whereas a closure is a function which knows which
    state is appropriate for it. If you use that definition, then you
    can't generate new closures in C, but you can create new objects,
    including objects that are a block of storage together with a single
    function. The difference is in the invocation, i.e.,whether you use

    (*closure_ptr)(newarguments)
    or
    (*object->function_pointer)(object, newarguments)


    Historically, the nested-function manifestation of closures has certainly
    been used many times: it isn't wrong, but it in -my- opinion, it
    is not complete.
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
     
    Walter Roberson, Mar 14, 2006
    #4
  5. Ben C Guest

    On 2006-03-14, Walter Roberson <-cnrc.gc.ca> wrote:

    > [...] The difference between a closure and an object is that an object
    > is understood to be a data entity which knows which functions are
    > appropriate for it, whereas a closure is a function which knows which
    > state is appropriate for it.


    This is a nice way of putting it.

    If C did allow you to return references to nested functions complete
    with their "environments", the compiler would have to allocate (and
    delete) memory for these environments and transfer objects from the
    stack frame into them. Building in this level of automatic run-time
    memory management would be against the spirit of C.
     
    Ben C, Mar 14, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mariusz

    explanation

    Mariusz, Jan 12, 2004, in forum: VHDL
    Replies:
    1
    Views:
    597
    tbx135
    Jan 13, 2004
  2. Just D.
    Replies:
    0
    Views:
    447
    Just D.
    Mar 10, 2006
  3. Buck Rogers

    explanation of a program required please

    Buck Rogers, Dec 23, 2003, in forum: C Programming
    Replies:
    6
    Views:
    399
    Joona I Palaste
    Jan 5, 2004
  4. explanation required???

    , Mar 18, 2006, in forum: C Programming
    Replies:
    5
    Views:
    302
    Keith Thompson
    Mar 19, 2006
  5. Will

    Prototyping - explanation required

    Will, Oct 29, 2003, in forum: Javascript
    Replies:
    1
    Views:
    96
    Lasse Reichstein Nielsen
    Oct 29, 2003
Loading...

Share This Page