For Programmers: Free Programming Magazines  


Home > Archive > C > February 2006 > private functions inside a function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author private functions inside a function
Grant Schoep

2006-02-26, 6:55 pm

I recently inherited some older C code and saw something I haven't really
seen before. I noticed it as it fails to compile at my home computer, as it
has a newer (4.x vs 3.x ) gcc compiler.

Its a private function, inside a function. Something like this.


void myFunc(...) {
static void myPrivateFunc(...) {
}
...
return;
}

I assume its failing to compile, because the private function is declared
static. I guess what I'm comfused at is I havent' really seen functions
declared in functions before. Could this have been done for performance
reasons or something? The original author has about 5 different C files,
each with one of these private functions, they all do about the same thing.
There basically some masking and quit math on the function parameters.

I'm just wondering if its performance as this is some intensive data
decoding code. Mucking with lots of data at the bit per bit level.
Guillaume

2006-02-26, 6:55 pm

Grant Schoep wrote:
> void myFunc(...) {
> static void myPrivateFunc(...) {
> }
> ...
> return;
> }
>
> I assume its failing to compile, because the private function is declared
> static.


No, it fails because nested functions are not allowed in standard C.
pemo

2006-02-26, 6:55 pm

Grant Schoep wrote:
> I recently inherited some older C code and saw something I haven't
> really seen before. I noticed it as it fails to compile at my home
> computer, as it has a newer (4.x vs 3.x ) gcc compiler.
>
> Its a private function, inside a function. Something like this.
>
>
> void myFunc(...) {
> static void myPrivateFunc(...) {
> }
> ...
> return;
> }
>
> I assume its failing to compile, because the private function is
> declared static. I guess what I'm comfused at is I havent' really
> seen functions declared in functions before. Could this have been
> done for performance reasons or something? The original author has
> about 5 different C files, each with one of these private functions,
> they all do about the same thing. There basically some masking and
> quit math on the function parameters.
>
> I'm just wondering if its performance as this is some intensive data
> decoding code. Mucking with lots of data at the bit per bit level.


Gnu C has an extension like this - so maybe it's some gcc code?


--
==============
Not a pedant
==============


osmium

2006-02-26, 6:55 pm

"pemo" writes:

> Grant Schoep wrote:
>
> Gnu C has an extension like this - so maybe it's some gcc code?


I think that's a good guess. You can put a function prototype within a C
function, but even that is rarely done. I guess it limits visibility. But
you can't put an actual function definition inside a function. It would be
nice if you could.


Pier Paolo

2006-02-26, 6:55 pm

Grant Schoep wrote:
> I recently inherited some older C code and saw something I haven't really
> seen before. I noticed it as it fails to compile at my home computer, as it
> has a newer (4.x vs 3.x ) gcc compiler.
>
> Its a private function, inside a function. Something like this.
>
>
> void myFunc(...) {
> static void myPrivateFunc(...) {
> }
> ...
> return;
> }
>
> I assume its failing to compile, because the private function is declared
> static. I guess what I'm comfused at is I havent' really seen functions
> declared in functions before. Could this have been done for performance
> reasons or something? The original author has about 5 different C files,
> each with one of these private functions, they all do about the same thing.
> There basically some masking and quit math on the function parameters.

does the author come from pascal development ?
>
> I'm just wondering if its performance as this is some intensive data
> decoding code. Mucking with lots of data at the bit per bit level.

Grant Schoep

2006-02-27, 6:57 pm

..
>
> Gnu C has an extension like this - so maybe it's some gcc code?
>
>



It is gnu, gnu 3.x seems to allow it. where as gnu 4.x does not. I seem to
remeber gnu 4.x talk that is getting much more adherent to standards.

I'm just going to pull the function out of the function. And keep it
"private" by not putting in the header. Since the same function, thought 5
different implementations, in 5 differetn files wil have the same name.
I'll just come up with a good uniq name for the 5 different functions. Its
actually fairly easy to test many aspects of this change, including
performance, as its a big data decom routine that we do analyize its
regular performance.
CBFalconer

2006-02-28, 6:55 pm

"Vladimir S. Oka" wrote:
> Grant Schoep wrote:
>
>
> Just declare them `static`. It'll make your function(s) "private",
> i.e. make them have internal linkage. Then you can have as many as
> you like with the same name (in different compilation units,
> obviously).


AND keep it out of the header.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com