Csound Csound-dev Csound-tekno Search About

[Cs-dev] how come this is allowed?

Date2008-01-09 11:04
FromVictor Lazzarini
Subject[Cs-dev] how come this is allowed?
I just stumbled across this:

int main() {

   int Hello() {
     int hello() {
       printf("hey\n");
     }
     printf("hi\n");

   }
   return 0;
}

and gcc compiled it.  It does not look like any C I've seen before. Is
this allowed in C99? I did not know it. Of course it bombs on the MS
compiler, which is only right. And g++ does not allow it, too, as I expected
from C++, but in C it seems crazy to me.

Victor
Victor Lazzarini
Music Technology Laboratory
Music Department
National University of Ireland, Maynooth


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 11:23
Fromroot
SubjectRe: [Cs-dev] how come this is allowed?
Looks like nonsense to me. If you can nest definition you need displays 
and/or static chains and that is a technology that C avoids for 
efficiency.  I guess in cases without non-local access it can be just name 
hiding which is OK, but not really in teh spirit of C

Not a C99 fan myself, but this is certainly illegal in C89

==John ff



-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 11:58
FromRichard Dobson
SubjectRe: [Cs-dev] how come this is allowed?
Found this on an IBM site:

"
A nested function is a function defined inside the definition of another 
function. It can be defined wherever a variable declaration is 
permitted, which allows nested functions within nested functions. Within 
the containing function, the nested function can be declared prior to 
being defined by using the auto keyword. Otherwise, a nested function 
has internal linkage. The language feature is an extension to C89 and 
C99, implemented to facilitate porting programs developed with GNU C.
..
"

So it's a Gnuism (one of many, I guess). It's also described on 
wikipedia at:

http://en.wikipedia.org/wiki/Nested_function

But it should be disabled by default; you only have to leave out a brace 
to get the error message about not allowing local function definitions.

Richard Dobson




root wrote:
> Looks like nonsense to me. If you can nest definition you need displays 
> and/or static chains and that is a technology that C avoids for 
> efficiency.  I guess in cases without non-local access it can be just name 
> hiding which is OK, but not really in teh spirit of C
> 
> Not a C99 fan myself, but this is certainly illegal in C89
> 
> ==John ff


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 12:14
FromVictor Lazzarini
SubjectRe: [Cs-dev] how come this is allowed?
But is this C99 or just gcc being naughty?

At 11:23 09/01/2008, you wrote:
>Looks like nonsense to me. If you can nest definition you need displays
>and/or static chains and that is a technology that C avoids for
>efficiency.  I guess in cases without non-local access it can be just name
>hiding which is OK, but not really in teh spirit of C
>
>Not a C99 fan myself, but this is certainly illegal in C89
>
>==John ff
>
>
>
>-------------------------------------------------------------------------
>Check out the new SourceForge.net Marketplace.
>It's the best place to buy or sell services for
>just about anything Open Source.
>http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
>_______________________________________________
>Csound-devel mailing list
>Csound-devel@lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/csound-devel

Victor Lazzarini
Music Technology Laboratory
Music Department
National University of Ireland, Maynooth


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 12:34
FromVictor Lazzarini
SubjectRe: [Cs-dev] how come this is allowed?
But it's not disabled by default, as it compiles with no extra options and 
gcc does not
even issue a warning. At least with mingwin.

At 11:58 09/01/2008, you wrote:
>Found this on an IBM site:
>
>"
>A nested function is a function defined inside the definition of another
>function. It can be defined wherever a variable declaration is
>permitted, which allows nested functions within nested functions. Within
>the containing function, the nested function can be declared prior to
>being defined by using the auto keyword. Otherwise, a nested function
>has internal linkage. The language feature is an extension to C89 and
>C99, implemented to facilitate porting programs developed with GNU C.
>..
>"
>
>So it's a Gnuism (one of many, I guess). It's also described on
>wikipedia at:
>
>http://en.wikipedia.org/wiki/Nested_function
>
>But it should be disabled by default; you only have to leave out a brace
>to get the error message about not allowing local function definitions.
>
>Richard Dobson
>
>
>
>
>root wrote:
> > Looks like nonsense to me. If you can nest definition you need displays
> > and/or static chains and that is a technology that C avoids for
> > efficiency.  I guess in cases without non-local access it can be just name
> > hiding which is OK, but not really in teh spirit of C
> >
> > Not a C99 fan myself, but this is certainly illegal in C89
> >
> > ==John ff
>
>
>-------------------------------------------------------------------------
>Check out the new SourceForge.net Marketplace.
>It's the best place to buy or sell services for
>just about anything Open Source.
>http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
>_______________________________________________
>Csound-devel mailing list
>Csound-devel@lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/csound-devel

Victor Lazzarini
Music Technology Laboratory
Music Department
National University of Ireland, Maynooth


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 18:16
FromRichard Dobson
SubjectRe: [Cs-dev] how come this is allowed?
Um, my mistake. It's disallowed by gcc on OSX (though I  found a web 
page that suggests they may bring it back as an option); but is alive 
and kicking on Linux gcc, and I can't even find an explicit flag to turn 
it off, apart presumably from -ansi etc.

It seems a lot of people out there do like it; but it raises questions 
of security (requires executable code on the stack) as well as of 
efficiency.


Richard Dobson


Victor Lazzarini wrote:
> But it's not disabled by default, as it compiles with no extra options and 
> gcc does not
> even issue a warning. At least with mingwin.
> 
> At 11:58 09/01/2008, you wrote:
> 


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 19:11
FromErik de Castro Lopo
SubjectRe: [Cs-dev] how come this is allowed?
Victor Lazzarini wrote:

> But is this C99 or just gcc being naughty?

Nested functions are allowed in many languages such as Pascal,
Modula2, Ada, Ocaml (my favourite) and Standard ML.

This however is a GNU C extention. Nested functions are not part
of any ISO C standard. See:

    http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

Erik
-- 
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"C++ is like jamming a helicopter inside a Miata and expecting
some sort of improvement." -- Drew Olbrich

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 22:43
FromJonatan Liljedahl
SubjectRe: [Cs-dev] how come this is allowed?
Richard Dobson wrote:
> Um, my mistake. It's disallowed by gcc on OSX (though I  found a web 
> page that suggests they may bring it back as an option); but is alive 
> and kicking on Linux gcc, and I can't even find an explicit flag to turn 
> it off, apart presumably from -ansi etc.
> 
> It seems a lot of people out there do like it; but it raises questions 
> of security (requires executable code on the stack) as well as of 
> efficiency.

I thought it just was a matter of scoping? that the functions code
aren't actually on the stack but in the usual code space, that it's just
a symbol table thing... no?

-- 
/Jonatan         [ http://kymatica.com ]

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 23:10
FromErik de Castro Lopo
SubjectRe: [Cs-dev] how come this is allowed?
Jonatan Liljedahl wrote:

> I thought it just was a matter of scoping? that the functions code
> aren't actually on the stack but in the usual code space, that it's just
> a symbol table thing... no?

Its a little more complicated than that.

The nested function can refer to automatic variables and function
parameters of the outer function *and*, the outer function can return
a pointer the the nested function.

The pointer to the returned function must still have access to the
automatic variables and function parameters which were on the stack
and are not accessible after the outer function has returned.

Gcc uses a techniques called trampolines:

    http://gcc.gnu.org/onlinedocs/gccint/Trampolines.html

to make all this black magic happen and work correctly.

I would probably not use stuff like this in C because it is not 
part of standard ISO C, but being able to do stuff like this is
extremely powerful in languages that do allow nested functions,
have functions as first class entity and have higher order
functions.

    http://en.wikipedia.org/wiki/First-class_object
    http://en.wikipedia.org/wiki/Higher-order_function

Erik
-- 
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"life is too long to be an expert at harmful things, including
such evilness as C++ and perl." -- Erik Naggum

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2008-01-09 23:43
FromJonatan Liljedahl
Subject[Cs-dev] OT: Re: how come this is allowed?
Erik de Castro Lopo wrote:
> Jonatan Liljedahl wrote:
> 
>> I thought it just was a matter of scoping? that the functions code
>> aren't actually on the stack but in the usual code space, that it's just
>> a symbol table thing... no?
> 
> Its a little more complicated than that.
> 
> The nested function can refer to automatic variables and function
> parameters of the outer function *and*, the outer function can return
> a pointer the the nested function.
> 
> The pointer to the returned function must still have access to the
> automatic variables and function parameters which were on the stack
> and are not accessible after the outer function has returned.

But do they? I mean you can't return a pointer to an automatic
allocation (alloca) since that variables space is freed at the return,
or return a pointer to one of the function parameters.

But if it works it's very cool :)

> Gcc uses a techniques called trampolines:
> 
>     http://gcc.gnu.org/onlinedocs/gccint/Trampolines.html
> 
> to make all this black magic happen and work correctly.
> 
> I would probably not use stuff like this in C because it is not 
> part of standard ISO C, but being able to do stuff like this is
> extremely powerful in languages that do allow nested functions,
> have functions as first class entity and have higher order
> functions.
> 
>     http://en.wikipedia.org/wiki/First-class_object
>     http://en.wikipedia.org/wiki/Higher-order_function

Yes, it's great. I use it all the time in the Nasal language
(www.plausible.org/nasal).

-- 
/Jonatan         [ http://kymatica.com ]

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net