mystery code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    #1

    mystery code?

    I ran across this code from a statistics library. The header
    source code was not available.

    long double __declspec(nake d) poisson_distrib ution(int k,long
    double m)
    {
    }
    long double pdtrl(int k,long double m )
    {
    long double v;

    if( (k < 0) || (m <= 0.0L) )
    {
    mtherr( "pdtrl", DOMAIN );
    return( 0.0L );
    }
    v = k+1;
    return( igamcl( v, m ) );
    }

    What would be the purpose of "__declspec(nak ed)" in the signature
    of poisson_distrib ution()? Is this legal C syntax? Why would the
    function body be empty?
  • John F

    #2
    Re: mystery code?


    "John Smith" wrote:[color=blue]
    >I ran across this code from a statistics library. The header source code
    >was not available.
    >
    > long double __declspec(nake d) poisson_distrib ution(int k,long double m)
    > {
    > }[/color]

    At least a return statement is missing...
    [color=blue]
    > long double pdtrl(int k,long double m )
    > {
    > long double v;
    >
    > if( (k < 0) || (m <= 0.0L) )
    > {
    > mtherr( "pdtrl", DOMAIN );
    > return( 0.0L );
    > }
    > v = k+1;
    > return( igamcl( v, m ) );
    > }
    >
    > What would be the purpose of "__declspec(nak ed)" in the signature of
    > poisson_distrib ution()?[/color]

    from the MSDN:
    For functions declared with the naked attribute, the compiler generates code
    without prolog and epilog code. You can use this feature to write your own
    prolog/epilog code sequences using inline assembler code. Naked functions
    are particularly useful in writing virtual device drivers.

    http://msdn.microsoft.com/library/de...m/msmod_25.asp
    [color=blue]
    > Is this legal C syntax?[/color]

    __declspec is not C. The rest is OK.
    [color=blue]
    > Why would the function body be empty?[/color]

    mostly to avoid errors or to tell the compiler that an assembler
    implementation exists, but in this case it will create new ones...

    --
    regards
    John


    Comment

    • Ben Pfaff

      #3
      Re: mystery code?

      John Smith <JSmith@mail.ne t> writes:
      [color=blue]
      > What would be the purpose of "__declspec(nak ed)" in the signature
      > of poisson_distrib ution()? Is this legal C syntax?[/color]

      __declspec is either an extension of a particular C
      implementation, or it's the name of a macro. It's not standard
      C.
      [color=blue]
      > Why would the function body be empty?[/color]

      Beats me.
      --
      "In My Egotistical Opinion, most people's C programs should be indented six
      feet downward and covered with dirt." -- Blair P. Houghton

      Comment

      • John F

        #4
        Re: mystery code?


        "Ben Pfaff" wrote:[color=blue]
        > John Smith writes:
        >[color=green]
        >> What would be the purpose of "__declspec(nak ed)" in the signature
        >> of poisson_distrib ution()? Is this legal C syntax?[/color]
        >
        > __declspec is either an extension of a particular C
        > implementation, or it's the name of a macro. It's not standard
        > C.
        >[color=green]
        >> Why would the function body be empty?[/color]
        >
        > Beats me.[/color]

        (together with __declspec(nake d) it makes sense if the function is
        implemented in assembler. the body is needed, since the __declspec(nake d)
        modifyer [OT, extension] is not allowed on prototypes in VC... that is why I
        was guessing at VC... directing to the MSDN)

        --
        John


        Comment

        • Rod Pemberton

          #5
          Re: mystery code?


          "John Smith" <JSmith@mail.ne t> wrote in message
          news:MOKJf.3944 3$sa3.37126@pd7 tw1no...[color=blue]
          > I ran across this code from a statistics library. The header
          > source code was not available.
          >
          > long double __declspec(nake d) poisson_distrib ution(int k,long
          > double m)
          > {
          > }
          > long double pdtrl(int k,long double m )
          > {
          > long double v;
          >
          > if( (k < 0) || (m <= 0.0L) )
          > {
          > mtherr( "pdtrl", DOMAIN );
          > return( 0.0L );
          > }
          > v = k+1;
          > return( igamcl( v, m ) );
          > }
          >
          > What would be the purpose of "__declspec(nak ed)" in the signature
          > of poisson_distrib ution()?[/color]

          It removes the prologue and epilogue (i.e., stack frame), allocation of
          variables, and the assembly language return statement for the procedure.
          This is useful for writing interrupt service routines since you can change
          the type of assembly language return, or preventing variables from being
          pushed onto a stack.
          [color=blue]
          > Is this legal C syntax?[/color]

          You'll see this with Microsoft and OpenWatcom compilers.
          [color=blue]
          > Why would the
          > function body be empty?[/color]

          It should be thought of as a compiler modifier, like GCC's __attribute__() ,
          which is setup similar to a prototype.


          Rod Pemberton



          Comment

          • Malcolm

            #6
            Re: mystery code?




            "John Smith" <JSmith@mail.ne t> wrote in message
            news:MOKJf.3944 3$sa3.37126@pd7 tw1no...[color=blue]
            >I ran across this code from a statistics library. The header source code
            >was not available.
            >
            > long double __declspec(nake d) poisson_distrib ution(int k,long double m)
            > {
            > }
            > long double pdtrl(int k,long double m )
            > {
            > long double v;
            >
            > if( (k < 0) || (m <= 0.0L) )
            > {
            > mtherr( "pdtrl", DOMAIN );
            > return( 0.0L );
            > }
            > v = k+1;
            > return( igamcl( v, m ) );
            > }
            >
            > What would be the purpose of "__declspec(nak ed)" in the signature of
            > poisson_distrib ution()? Is this legal C syntax? Why would the function
            > body be empty?
            >[/color]
            __declspec(nake d) is some sort of compiler-specific gibberish to make C
            integrate with non-C code.
            It is not legal, portable ANSI C, and won't compile on another compiler.
            That's not the same as saying that it is bad code.

            I would guess that the empty function body is a placeholder and the real
            code is not written in C.[color=blue]
            >[/color]
            --
            Buy my book 12 Common Atheist Arguments (refuted)
            $1.25 download or $6.90 paper, available www.lulu.com


            Comment

            • Dave Thompson

              #7
              Re: mystery code?

              On Sat, 18 Feb 2006 19:44:44 GMT, John Smith <JSmith@mail.ne t> wrote:
              [color=blue]
              > I ran across this code from a statistics library. The header
              > source code was not available.
              >
              > long double __declspec(nake d) poisson_distrib ution(int k,long
              > double m)
              > {
              > }
              > long double pdtrl(int k,long double m )
              > {[/color]
              <snip>[color=blue]
              >
              > What would be the purpose of "__declspec(nak ed)" in the signature
              > of poisson_distrib ution()? Is this legal C syntax? Why would the
              > function body be empty?[/color]

              As others have said, __declspec is not standard, it's an M$VC feature
              perhaps adopted by some others for compatibility. In this case on that
              implementation I'd bet 'naked' results in no generated code at all for
              poisson_distrib ution just the entrypoint symbol, with the result that
              calling that name actually results in calling (the body of) pdtr(),
              which has the same parameters and return type and thus works --
              assuming whatever igamcl() returns is in fact correct.

              - David.Thompson1 at worldnet.att.ne t

              Comment

              Working...