473,587 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1822
On 2006-03-14, aa*****@gmail.c om <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.c om <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************ **********@i40g 2000cwc.googleg roups.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_point er)(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.n rc-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 "environmen ts", 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
2510
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 explain instance variables. What I'm looking for is the best compromise between brevity and a full explanation. The students are non-CIS technical...
2
1804
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 seves as t the 'one' side of one-to-many relationship. Enter a record in the 'one' side table with the desired key value, and then make the entry...
1
3335
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 Function explanation Part II
6
1949
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
2762
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() { int val=5; printf("%d %d %d %d",val,--val,++val,val--); } under turbo compiler its giving
14
9727
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
1543
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: "If there are external markup declarations but there is no standalone document declaration, the value "no" is assumed."
6
350
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
3985
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 = document.getElementsByTagName("form"); alert(myForm.stateId.value); myForm.removeChild(myForm.stateId);
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.