private functions inside a function

P

Pier Paolo

Grant said:
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 ?
 
G

Grant Schoep

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

Guillaume

Grant said:
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.
 
P

pemo

Grant said:
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?
 
O

osmium

pemo said:
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.
 
G

Grant Schoep

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

Vladimir S. Oka

Grant said:
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.

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

Keith Thompson

Vladimir S. Oka said:
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).

One thing that nested functions give you is the ability to refer to
declarations in the enclosing function. If your nested functions do
that, you'll need to find some other way to get to the information.

"Logic is a little bird, tweeting in a meadow. Logic is a wreath of
pretty flowers which smell *bad*. Are your circuits registering
correctly? Your ears are green!" (If I recall correctly.)
 
V

Vladimir S. Oka

Keith said:
One thing that nested functions give you is the ability to refer to
declarations in the enclosing function. If your nested functions do
that, you'll need to find some other way to get to the information.

Good point. I must admit I sometimes miss Pascal-like ability to nest
functions. I don't think that adding this to C would break any existing
programs. or would it?
"Logic is a little bird, tweeting in a meadow. Logic is a wreath of
pretty flowers which smell *bad*. Are your circuits registering
correctly? Your ears are green!" (If I recall correctly.)

Didn't know that. My sigs come from fortune (I'm cheap). ;-)
 
C

CBFalconer

Vladimir S. Oka said:
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/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top