private functions inside a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant Schoep

    private functions inside a function

    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

    #2
    Re: private functions inside a function

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

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

    Comment

    • pemo

      #3
      Re: private functions inside a function

      Grant Schoep wrote:[color=blue]
      > 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.[/color]

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


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


      Comment

      • osmium

        #4
        Re: private functions inside a function

        "pemo" writes:
        [color=blue]
        > Grant Schoep wrote:[color=green]
        >> 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.[/color]
        >
        > Gnu C has an extension like this - so maybe it's some gcc code?[/color]

        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.


        Comment

        • Pier Paolo

          #5
          Re: private functions inside a function

          Grant Schoep wrote:[color=blue]
          > 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.[/color]
          does the author come from pascal development ?[color=blue]
          >
          > 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.[/color]

          Comment

          • Grant Schoep

            #6
            Re: private functions inside a function

            ..[color=blue]
            >
            > Gnu C has an extension like this - so maybe it's some gcc code?
            >
            >[/color]


            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.

            Comment

            • Vladimir S. Oka

              #7
              Re: private functions inside a function

              Grant Schoep wrote:[color=blue]
              > 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.[/color]

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

              --
              BR, Vladimir

              Logic is a little bird, sitting in a tree; that smells *awful*.

              Comment

              • Keith Thompson

                #8
                Re: private functions inside a function

                "Vladimir S. Oka" <novine@btopenw orld.com> writes:[color=blue]
                > Grant Schoep wrote:[color=green]
                >> 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.[/color]
                >
                > 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).[/color]

                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.
                [color=blue]
                > --
                > BR, Vladimir
                >
                > Logic is a little bird, sitting in a tree; that smells *awful*.[/color]

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

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Vladimir S. Oka

                  #9
                  Re: private functions inside a function

                  Keith Thompson wrote:
                  [color=blue]
                  > "Vladimir S. Oka" <novine@btopenw orld.com> writes:[color=green]
                  >> Grant Schoep wrote:[color=darkred]
                  >>> 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.[/color]
                  >>
                  >> 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).[/color]
                  >
                  > 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.[/color]

                  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?
                  [color=blue][color=green]
                  >> Logic is a little bird, sitting in a tree; that smells *awful*.[/color]
                  >
                  > "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.)[/color]

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

                  --
                  BR, Vladimir

                  Humpty Dumpty was pushed.

                  Comment

                  • CBFalconer

                    #10
                    Re: private functions inside a function

                    "Vladimir S. Oka" wrote:[color=blue]
                    > Grant Schoep wrote:
                    >[color=green]
                    >> 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.[/color]
                    >
                    > 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).[/color]

                    AND keep it out of the header.

                    --
                    "If you want to post a followup via groups.google.c om, 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/googlegroupsrep ly/>


                    Comment

                    Working...