C/C++ guidelines

Discussion in 'C++' started by Robbie Marshall, Sep 15, 2007.

  1. Hi groups,

    I'm about to embark on a new project and for maximum portability we've
    been asked to code it in the common subset of C and C++.

    Can anyone suggest any good documents that might help us? Lists of
    gotchas to avoid, that sort of thing?

    TIA!

    Robbie
    Robbie Marshall, Sep 15, 2007
    #1
    1. Advertising

  2. Robbie Marshall

    Ian Collins Guest

    Robbie Marshall wrote:
    > Hi groups,
    >
    > I'm about to embark on a new project and for maximum portability we've
    > been asked to code it in the common subset of C and C++.
    >

    For maximum portability, code in C89.

    If you aren't aware of the (sometimes subtle) differences between C and
    C++ (even within the common subset), don't risk running into them! Even
    when using the common subset, there are idiomatic differences between
    the two.

    --
    Ian Collins.
    Ian Collins, Sep 15, 2007
    #2
    1. Advertising

  3. In article <>,
    Ian Collins <> wrote:

    >> I'm about to embark on a new project and for maximum portability we've
    >> been asked to code it in the common subset of C and C++.


    >For maximum portability, code in C89.


    That's not enough: for example you need to avoid C++ keywords
    ("namespace" is one I've run into).

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Sep 15, 2007
    #3
  4. Robbie Marshall

    Guest

    On 2007-09-15, Robbie Marshall <> wrote:
    > Hi groups,
    >
    > I'm about to embark on a new project and for maximum portability we've
    > been asked to code it in the common subset of C and C++.
    >
    > Can anyone suggest any good documents that might help us? Lists of
    > gotchas to avoid, that sort of thing?
    >


    Could you give us a little more context? Why do you want to program in the
    subset of both languages? It seems to me that's a bit of a waste. You're trying
    to write code in a language that's not really standard, if you want long term
    comaptibility you should take a standardized language and go for it. If you're
    willing to take a subset of C++ and C, you might as well just program in plain
    C. If you need to you can easily import your C functions in C++ using extern
    "C".

    I don't really know how to answer your questions, i.e. I don't know any
    documentation on writting code in a subset of C and C++. But I really feel you
    should rethink what you want to do, and if you don't feel the same way I'm
    really curious to know why you don't agree with me.
    , Sep 15, 2007
    #4
  5. * Robbie Marshall:
    >
    > I'm about to embark on a new project and for maximum portability we've
    > been asked to code it in the common subset of C and C++.
    >
    > Can anyone suggest any good documents that might help us? Lists of
    > gotchas to avoid, that sort of thing?


    Coding in a common subset of C and C++ is a gotcha.

    C and C++ are two different languages, where some constructs that are
    valid in both mean different things. Recommended coding style therefore
    differs. E.g, in C you should not cast the result of malloc, because
    doing that might introduce subtle errors, while in C++ you must.

    Inform your manager.

    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Sep 15, 2007
    #5
  6. Robbie Marshall

    Ark Khasin Guest

    Richard Tobin wrote:
    > In article <>,
    > Ian Collins <> wrote:
    >
    >>> I'm about to embark on a new project and for maximum portability we've
    >>> been asked to code it in the common subset of C and C++.

    >
    >> For maximum portability, code in C89.

    >
    > That's not enough: for example you need to avoid C++ keywords
    > ("namespace" is one I've run into).


    Is it safe to code in C89/90/94 (corrigenda) and compile, or better yet
    lint as C++ AND C?
    Is it the same labor as to to write in a common subset of British and
    American and other flavors of English?
    --
    Ark
    Ark Khasin, Sep 16, 2007
    #6
  7. Robbie Marshall

    Phlip Guest

    Robbie Marshall wrote:

    > I'm about to embark on a new project and for maximum portability we've
    > been asked to code it in the common subset of C and C++.


    For maximum portability, write unit tests for all your code. Then you can
    run the tests on each target platform.

    For _real_ portability (not just "tell our boss we are portable"
    portability), you could configure a test server on each target platform. Run
    the test suite each time you integrate, automatically on each platform.
    Configure them to e-mail all the developers if the tests break on any
    platform.

    Yes, I've done this before...

    --
    Phlip
    http://www.oreilly.com/catalog/9780596510657/
    "Test Driven Ajax (on Rails)"
    assert_xpath, assert_javascript, & assert_ajax
    Phlip, Sep 16, 2007
    #7
  8. Robbie Marshall

    Phlip Guest

    Wade Ward wrote:

    >> Yes, I've done this before...


    > I think the common subset of c and c++ is empty, because if you do malloc
    > then yikes and if you don't malloc, well that's not much of a program is
    > it.


    Actually I haven't done the Common Subset thing.

    But (char*)malloc(..) is well-formed C++ (with appropriate substitution for
    ....). Don't sweat the details; portability is up to your compiler(s).

    --
    Phlip
    http://www.oreilly.com/catalog/9780596510657/
    "Test Driven Ajax (on Rails)"
    assert_xpath, assert_javascript, & assert_ajax
    Phlip, Sep 16, 2007
    #8
  9. Robbie Marshall

    CBFalconer Guest

    Phlip wrote:
    > Wade Ward wrote:
    >
    >>> Yes, I've done this before...

    >
    >> I think the common subset of c and c++ is empty, because if you
    >> do malloc then yikes and if you don't malloc, well that's not
    >> much of a program is it.

    >
    > Actually I haven't done the Common Subset thing.
    >
    > But (char*)malloc(..) is well-formed C++ (with appropriate
    > substitution for ...). Don't sweat the details; portability is up
    > to your compiler(s).


    And it is execreble dangerous coding in C. The languages are
    different. This is c.l.c, and c.l.c++ is a separate newsgroup
    dealing with a separate language. F'ups set.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 16, 2007
    #9
  10. Robbie Marshall

    Wade Ward Guest

    "Phlip" <> wrote in message
    news:...
    > Robbie Marshall wrote:
    >
    >> I'm about to embark on a new project and for maximum portability we've
    >> been asked to code it in the common subset of C and C++.

    >
    > For maximum portability, write unit tests for all your code. Then you can
    > run the tests on each target platform.
    >
    > For _real_ portability (not just "tell our boss we are portable"
    > portability), you could configure a test server on each target platform.
    > Run the test suite each time you integrate, automatically on each
    > platform. Configure them to e-mail all the developers if the tests break
    > on any platform.
    >
    > Yes, I've done this before...

    I think the common subset of c and c++ is empty, because if you do malloc
    then yikes and if you don't malloc, well that's not much of a program is it.
    --
    Wade Ward

    'Wheat is for man.'
    --Joseph Smith>
    Wade Ward, Sep 16, 2007
    #10
  11. Robbie Marshall

    Phlip Guest

    CBFalconer wrote:

    > And it is execreble dangerous coding in C.


    Not sure why it's bad C - if you refer to the (char *)malloc typecast.

    Note I said, upthread, to write unit tests. This implies learning exactly
    what your compilers are doing, and matching the test runs on each platform
    that you _really_ want to port to. Portability is a process, not a
    guestimate based on all the alleged standards.

    --
    Phlip
    Phlip, Sep 16, 2007
    #11
  12. Robbie Marshall

    Ian Collins Guest

    Phlip wrote:
    > CBFalconer wrote:
    >
    >> And it is execreble dangerous coding in C.

    >
    > Not sure why it's bad C - if you refer to the (char *)malloc typecast.
    >

    The stock answer is it hides compiler warnings with some compilers when
    <stdlib.h> isn't included. In C, if undeclared, a function is assumed
    to return int (the old implicit int rule). Where sizeof(int) !=
    sizeof(void*), this can have very odd consequences.

    At least we don't have to worry about implicit int in C++.

    > Note I said, upthread, to write unit tests. This implies learning exactly
    > what your compilers are doing, and matching the test runs on each platform
    > that you _really_ want to port to. Portability is a process, not a
    > guestimate based on all the alleged standards.
    >

    While true, it's better to let the compiler find this kind of bug.

    --
    Ian Collins.
    Ian Collins, Sep 16, 2007
    #12
  13. Robbie Marshall

    Flash Gordon Guest

    Ark Khasin wrote, On 16/09/07 02:40:
    > Richard Tobin wrote:
    >> In article <>,
    >> Ian Collins <> wrote:
    >>
    >>>> I'm about to embark on a new project and for maximum portability we've
    >>>> been asked to code it in the common subset of C and C++.

    >>
    >>> For maximum portability, code in C89.

    >>
    >> That's not enough: for example you need to avoid C++ keywords
    >> ("namespace" is one I've run into).

    >
    > Is it safe to code in C89/90/94 (corrigenda) and compile, or better yet
    > lint as C++ AND C?


    No. Since some code which is well formed in both languages will do
    different things in the two languages. We have has some such examples
    posted in comp.lang.c in the past week.

    > Is it the same labor as to to write in a common subset of British and
    > American and other flavors of English?


    The common subset changes.
    --
    Flash Gordon
    Flash Gordon, Sep 16, 2007
    #13
  14. Robbie Marshall <> writes:
    > I'm about to embark on a new project and for maximum portability we've
    > been asked to code it in the common subset of C and C++.
    >
    > Can anyone suggest any good documents that might help us? Lists of
    > gotchas to avoid, that sort of thing?


    Consider programming in C90 and making sure not to accidentally use a
    C++ compiler. You can enforce this with:

    #ifdef __cplusplus
    #error "Use a C compiler, not a C++ compiler"
    #endif

    --
    Keith Thompson (The_Other_Keith) <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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 16, 2007
    #14
  15. Robbie Marshall

    Wade Ward Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Robbie Marshall <> writes:
    >> I'm about to embark on a new project and for maximum portability we've
    >> been asked to code it in the common subset of C and C++.
    >>
    >> Can anyone suggest any good documents that might help us? Lists of
    >> gotchas to avoid, that sort of thing?

    >
    > Consider programming in C90 and making sure not to accidentally use a
    > C++ compiler. You can enforce this with:
    >
    > #ifdef __cplusplus
    > #error "Use a C compiler, not a C++ compiler"
    > #endif

    Keith's right here.

    Mr. Thompson has called me a troll, a curious appellation for a large,
    jellyfilled donut whose first german language book was Gödel, Escher, Bach.
    Was ist die Abbildung an Bloop, Floop, und Gloop?
    --
    Wade Ward

    "A sequence of zeroes and ones;" Indiana
    Wade Ward, Sep 16, 2007
    #15
  16. Robbie Marshall

    Chris Hills Guest

    In article <>, Robbie Marshall
    <> writes
    >Hi groups,
    >
    >I'm about to embark on a new project and for maximum portability we've
    >been asked to code it in the common subset of C and C++.
    >
    >Can anyone suggest any good documents that might help us? Lists of
    >gotchas to avoid, that sort of thing?


    Firstly what are the compilers and targets?

    Due to the lack of serious take up of the current C99 standard and many
    compiler having compiler and target extensions (also domain extensions)
    it is better to start with the tools than the standard.

    You have asked a practical not theoretical question.


    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris Hills, Sep 16, 2007
    #16
  17. "Wade Ward" <> writes:
    [...]
    > Mr. Thompson has called me a troll,

    [...]

    I don't believe I have.

    --
    Keith Thompson (The_Other_Keith) <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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 16, 2007
    #17
  18. Robbie Marshall

    Jim Langston Guest

    "Wade Ward" <> wrote in message
    news:...
    >
    >
    > "Keith Thompson" <> wrote in message
    > news:...
    >> Robbie Marshall <> writes:
    >>> I'm about to embark on a new project and for maximum portability we've
    >>> been asked to code it in the common subset of C and C++.
    >>>
    >>> Can anyone suggest any good documents that might help us? Lists of
    >>> gotchas to avoid, that sort of thing?

    >>
    >> Consider programming in C90 and making sure not to accidentally use a
    >> C++ compiler. You can enforce this with:
    >>
    >> #ifdef __cplusplus
    >> #error "Use a C compiler, not a C++ compiler"
    >> #endif

    > Keith's right here.
    >
    > Mr. Thompson has called me a troll, a curious appellation for a large,
    > jellyfilled donut whose first german language book was Gödel, Escher,
    > Bach. Was ist die Abbildung an Bloop, Floop, und Gloop?


    Ummm.. how do you take his response as him calling you a troll? His
    response was appropriate and gave some good advice.

    Seeing how you seem to think that's calling you a troll makes me think that
    maybe you are.
    Jim Langston, Sep 16, 2007
    #18
  19. Robbie Marshall

    Guest

    On Sep 16, 3:18 am, Robbie Marshall <> wrote:
    > Hi groups,
    >
    > I'm about to embark on a new project and for maximum portability we've
    > been asked to code it in the common subset of C and C++.
    >
    > Can anyone suggest any good documents that might help us? Lists of
    > gotchas to avoid, that sort of thing?
    >
    > TIA!
    >
    > Robbie
    , Sep 16, 2007
    #19
  20. Robbie Marshall

    Army1987 Guest

    On Sun, 16 Sep 2007 09:27:12 +0100, Flash Gordon wrote:
    > Ark Khasin wrote, On 16/09/07 02:40:
    >> Is it safe to code in C89/90/94 (corrigenda) and compile, or better yet
    >> lint as C++ AND C?

    > No. Since some code which is well formed in both languages will do
    > different things in the two languages. We have has some such examples
    > posted in comp.lang.c in the past week.

    There is code which is well formed in both C90 and C99 which will
    do different things in the two versions of the language, but I've
    never heard anyone suggesting *not* to try to write code in their
    common subset.

    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
    Army1987, Sep 16, 2007
    #20
    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. JIT

    Code Guidelines

    JIT, Oct 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    522
    Rajesh Kumar
    Nov 2, 2004
  2. Francisco Camarero

    VHDL Coding Guidelines

    Francisco Camarero, Jul 8, 2003, in forum: VHDL
    Replies:
    1
    Views:
    2,071
  3. Roger

    Portable Coding Guidelines?

    Roger, Dec 17, 2004, in forum: VHDL
    Replies:
    0
    Views:
    547
    Roger
    Dec 17, 2004
  4. Mike Kruchten

    Are there any guidelines for source control?

    Mike Kruchten, Apr 30, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    328
    John Saunders
    May 3, 2004
  5. sk
    Replies:
    5
    Views:
    2,254
    Shardul Kulkarni
    Mar 10, 2005
Loading...

Share This Page