mystery code?

Discussion in 'C Programming' started by John Smith, Feb 18, 2006.

  1. John Smith

    John Smith Guest

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

    long double __declspec(naked) poisson_distribution(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(naked)" in the signature
    of poisson_distribution()? Is this legal C syntax? Why would the
    function body be empty?
     
    John Smith, Feb 18, 2006
    #1
    1. Advertising

  2. John Smith

    John F Guest

    "John Smith" wrote:
    >I ran across this code from a statistics library. The header source code
    >was not available.
    >
    > long double __declspec(naked) poisson_distribution(int k,long double m)
    > {
    > }


    At least a return statement is missing...

    > 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(naked)" in the signature of
    > poisson_distribution()?


    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/default.asp?url=/library/en-us/vccelng/htm/msmod_25.asp

    > Is this legal C syntax?


    __declspec is not C. The rest is OK.

    > Why would the function body be empty?


    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
     
    John F, Feb 18, 2006
    #2
    1. Advertising

  3. John Smith

    Ben Pfaff Guest

    John Smith <> writes:

    > What would be the purpose of "__declspec(naked)" in the signature
    > of poisson_distribution()? Is this legal C syntax?


    __declspec is either an extension of a particular C
    implementation, or it's the name of a macro. It's not standard
    C.

    > Why would the function body be empty?


    Beats me.
    --
    "In My Egotistical Opinion, most people's C programs should be indented six
    feet downward and covered with dirt." -- Blair P. Houghton
     
    Ben Pfaff, Feb 18, 2006
    #3
  4. John Smith

    John F Guest

    "Ben Pfaff" wrote:
    > John Smith writes:
    >
    >> What would be the purpose of "__declspec(naked)" in the signature
    >> of poisson_distribution()? Is this legal C syntax?

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

    >
    > Beats me.


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

    --
    John
     
    John F, Feb 18, 2006
    #4
  5. "John Smith" <> wrote in message
    news:MOKJf.39443$sa3.37126@pd7tw1no...
    > I ran across this code from a statistics library. The header
    > source code was not available.
    >
    > long double __declspec(naked) poisson_distribution(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(naked)" in the signature
    > of poisson_distribution()?


    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.

    > Is this legal C syntax?


    You'll see this with Microsoft and OpenWatcom compilers.

    > Why would the
    > function body be empty?


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


    Rod Pemberton
     
    Rod Pemberton, Feb 18, 2006
    #5
  6. John Smith

    Malcolm Guest

    "John Smith" <> wrote in message
    news:MOKJf.39443$sa3.37126@pd7tw1no...
    >I ran across this code from a statistics library. The header source code
    >was not available.
    >
    > long double __declspec(naked) poisson_distribution(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(naked)" in the signature of
    > poisson_distribution()? Is this legal C syntax? Why would the function
    > body be empty?
    >

    __declspec(naked) 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.
    >

    --
    Buy my book 12 Common Atheist Arguments (refuted)
    $1.25 download or $6.90 paper, available www.lulu.com
     
    Malcolm, Feb 19, 2006
    #6
  7. On Sat, 18 Feb 2006 19:44:44 GMT, John Smith <> wrote:

    > I ran across this code from a statistics library. The header
    > source code was not available.
    >
    > long double __declspec(naked) poisson_distribution(int k,long
    > double m)
    > {
    > }
    > long double pdtrl(int k,long double m )
    > {

    <snip>
    >
    > What would be the purpose of "__declspec(naked)" in the signature
    > of poisson_distribution()? Is this legal C syntax? Why would the
    > function body be empty?


    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_distribution 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.net
     
    Dave Thompson, Feb 27, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    another array ranges mystery

    valentin tihomirov, Jun 18, 2005, in forum: VHDL
    Replies:
    2
    Views:
    491
    Mike Treseler
    Jun 18, 2005
  2. S Austin
    Replies:
    1
    Views:
    355
    Victor Bazarov
    Jan 29, 2004
  3. jimmyjoe

    Strange code - mystery

    jimmyjoe, Jul 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    407
    jimmyjoe
    Jul 27, 2006
  4. Uncle Ben

    mystery string code - help!

    Uncle Ben, Apr 20, 2011, in forum: Python
    Replies:
    2
    Views:
    260
    Uncle Ben
    Apr 21, 2011
  5. wkevin
    Replies:
    4
    Views:
    607
    Edward A. Falk
    Nov 7, 2011
Loading...

Share This Page