473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

explanation required

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

Mar 14 '06 #1
4 1812
On 2006-03-14, aa*****@gmail.com <aa*****@gmail.com> 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.
Mar 14 '06 #2
On 2006-03-14, aa*****@gmail.com <aa*****@gmail.com> 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)
Mar 14 '06 #3
In article <11**********************@i40g2000cwc.googlegroups .com>,
<aa*****@gmail.com> 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
Mar 14 '06 #4
On 2006-03-14, Walter Roberson <ro******@ibd.nrc-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.
Mar 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
2
by: Susan Bricker | last post by:
I went back to read my post and found an error in my description ... here is the post, again, corrected: The following error: "The current field must match the join key '?' in the table that...
1
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
6
by: Buck Rogers | last post by:
Hi guys! Love your work! The below program is from K&R2, p22. ================================= #include <stdio.h> /* count digits, white space, others */ main() {
70
by: rahul8143 | last post by:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main()...
14
by: Akhil | last post by:
plz c d following code #include<stdio.h> void main() { int x=4,y=1; y=x++++; //gives error message lvalue required y=x++ + ++y;//no errors
1
by: VK | last post by:
It is possibly more suitable to address this question to W3C mailing list, but I'm trying here first. Could anyone comment on <http://www.w3.org/TR/REC-xml/#sec-rmd> The first statement says:...
6
by: Sree | last post by:
If the program (myprog) is run from the command line as myprog 1 2 3 , What would be the output? main(int argc, char *argv) { int i; for(i=0;i<argc;i++) printf("%s",argv); }
11
by: Naeem | last post by:
I have a Javascript function, which changes a text field of a form into a select field. Following is the function function changeStateField() { var myForm =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.