Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > explanation required

Reply
Thread Tools

explanation required

 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      03-14-2006
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.

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...ter_science%29

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

 
Reply With Quote
 
 
 
 
Ben C
Guest
Posts: n/a
 
      03-14-2006
On 2006-03-14, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
David Holland
Guest
Posts: n/a
 
      03-14-2006
On 2006-03-14, (E-Mail Removed) <(E-Mail Removed)> wrote:
> 2)what exactly is this closure???
>
> http://en.wikipedia.org/wiki/Closure...ter_science%29
>
> 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)
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-14-2006
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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...ter_science%29


> 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
 
Reply With Quote
 
Ben C
Guest
Posts: n/a
 
      03-14-2006
On 2006-03-14, Walter Roberson <(E-Mail Removed)-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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explanation required??? aarklon@gmail.com C Programming 5 03-19-2006 02:44 AM
Create a VD with required properties on a required Web Server (Windows Advanced Server 2000 has 3 Web Servers) Just D. ASP .Net 0 03-10-2006 06:02 AM
DVD disks explanation required Emrys Davies Computer Support 6 11-24-2005 08:29 PM
explanation of a program required please Buck Rogers C Programming 6 01-05-2004 04:43 PM
Prototyping - explanation required Will Javascript 1 10-29-2003 07:24 PM



Advertisments