explanation required

A

aarklon

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
 
B

Ben C

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.
 
D

David Holland

> 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.
 
W

Walter Roberson

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???

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.
 
B

Ben C

[...] 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top