Reading BSD system code

Discussion in 'C Programming' started by istillshine@gmail.com, Apr 4, 2008.

  1. Guest

    In string.h, I saw

    __BEGIN_DECLS

    ....

    __END_DECLS

    What's the use of these labels? And why did they always put two
    underscores before a name?

    I also saw a function prototype declaration:

    strlen(const char *) __pure;

    Why adding __pure?
    , Apr 4, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > In string.h, I saw
    >
    > __BEGIN_DECLS
    >
    > ....
    >
    > __END_DECLS
    >
    > What's the use of these labels? And why did they always put two
    > underscores before a name?
    >
    > I also saw a function prototype declaration:
    >
    > strlen(const char *) __pure;
    >
    > Why adding __pure?
    >

    Try searching for the definition of these symbols in the code, they will
    probably be documented.

    Alternatively as none of these are standard C, try as BSD group.

    --
    Ian Collins.
    Ian Collins, Apr 4, 2008
    #2
    1. Advertising

  3. In article <>,
    <> wrote:
    >In string.h, I saw


    >__BEGIN_DECLS
    >...
    >__END_DECLS


    >What's the use of these labels?


    I don't know; they aren't part of standard C.

    >And why did they always put two
    >underscores before a name?


    Because two underscores indicates a macro or identifier whose name
    is reserved for use by the implementation. If they had not put the
    two underscores in (and had also not used one of the reserved
    formats that starts with a single underscore) then they would have
    risked using an identifier defined by the user.


    >I also saw a function prototype declaration:


    >strlen(const char *) __pure;


    >Why adding __pure?


    Also not part of standard C. We could speculate, though, that to
    that particular implementation, it means that the routine has no
    side effects.
    --
    "There's no term to the work of a scientist." -- Walter Reisch
    Walter Roberson, Apr 4, 2008
    #3
  4. On 4 Apr 2008 at 3:00, Walter Roberson wrote:
    > In article <>,
    > <> wrote:
    >>In string.h, I saw

    >
    >>__BEGIN_DECLS
    >>...
    >>__END_DECLS

    >
    >>What's the use of these labels?

    >
    > I don't know; they aren't part of standard C.


    I think a common use for macros like this is to facilitate sharing
    headers between C and C++ by defining them something like this:

    #ifdef __cplusplus
    #define __BEGIN_DECLS extern "C" {
    #define __END_DECLS }
    #else
    #define __BEGIN_DECLS
    #define __END_DECLS
    #endif

    The OP could check whether this is true in his case by grepping for
    something like "#define *__BEGIN_DECLS" in his BSD headers.

    >>I also saw a function prototype declaration:

    >
    >>strlen(const char *) __pure;

    >
    >>Why adding __pure?

    >
    > Also not part of standard C. We could speculate, though, that to
    > that particular implementation, it means that the routine has no
    > side effects.


    Specifically, this informs the compiler that it can optimize away calls
    to strlen. If there's also
    my_do_all_function(const char *filename);
    and I have code like

    char *s=get_input();
    strlen(s); /* discard the return value */
    my_do_all_function(s); /* discard the return value */

    then the fact that strlen is pure tells the compiler it doesn't need to
    include code for the strlen call, whereas it can't just get rid of the
    call to my_do_all_function because it might have side effects.

    You can also add __pure to your own function definitions, but for
    maximum portability (other compilers may have a different syntax, or not
    support it at all) use preprocessor macros like:

    #ifdef __foo_bar_compiler
    #define PURE __pure
    #else
    #define PURE
    #endif

    int simple_count(const char **s, size_t n) PURE
    {
    /* function with no side_effects */
    }
    Antoninus Twink, Apr 4, 2008
    #4
  5. In article <>,
    <> wrote:

    >In string.h, I saw
    >
    >__BEGIN_DECLS
    >
    >...
    >
    >__END_DECLS
    >
    >What's the use of these labels?


    They are macros. If you look at their definitions (which is in the
    file included just beforehand, sys/cdefs.h) you'll see that they
    expand either to nothing, or if __cplusplus is defined, to 'extern "C"
    {' and '}'. That is, they're wrapping the declarations in something
    suitable for inclusion in C++ programs.

    >strlen(const char *) __pure;
    >
    >Why adding __pure?


    It relates to a gcc extension and is defined differently depending on
    the version of gcc used. Again, see sys/cdefs.h for the definition.

    -- Richard
    --
    :wq
    Richard Tobin, Apr 4, 2008
    #5
    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. Chameleon

    BSD ImageResize library in J2ME

    Chameleon, Mar 25, 2006, in forum: Java
    Replies:
    3
    Views:
    734
    Roedy Green
    Mar 25, 2006
  2. Daniel T.

    [OT] BSD Socket question...

    Daniel T., Aug 31, 2003, in forum: C++
    Replies:
    2
    Views:
    382
    llewelly
    Sep 1, 2003
  3. Abby
    Replies:
    1
    Views:
    408
    Sidney Cadot
    Nov 15, 2003
  4. Elf M. Sternberg

    Python signal delivery under BSD 4.4

    Elf M. Sternberg, Aug 12, 2003, in forum: Python
    Replies:
    8
    Views:
    346
    Donn Cave
    Aug 16, 2003
  5. j_mckitrick
    Replies:
    0
    Views:
    315
    j_mckitrick
    May 14, 2004
Loading...

Share This Page