A concern about mixing C and C++

Discussion in 'C++' started by ziman137, Jul 29, 2006.

  1. ziman137

    ziman137 Guest

    Hello all,

    I have a question and am seeking for some advice.

    I am currently working to implement an algorithmic library. Because
    the performance is the most important factor in later applications, I
    decide to write it in C instead of C++. However, I thought it might be
    convenient to use some C++ code at some misc places. I'm aware that, I
    could always use the C++ compiler to get it work.

    My concerns are:

    1) Would the way I mix C and C++ code have any potential drawbacks to
    the performance?
    2) Would the way I mix C and C++ code have any potential drawbacks for
    the future users to use the library?

    My intention for choosing C interface instead of C++ OOD is to gain the
    maximum performance as possible, yet I still like to use some C++
    coding features (e.g., "const", reference instead of pointers, ...).

    Thanks,

    Gary
     
    ziman137, Jul 29, 2006
    #1
    1. Advertising

  2. ziman137

    Phlip Guest

    ziman137 wrote:

    > I have a question and am seeking for some advice.
    >
    > I am currently working to implement an algorithmic library. Because
    > the performance is the most important factor in later applications, I
    > decide to write it in C instead of C++. However, I thought it might be
    > convenient to use some C++ code at some misc places. I'm aware that, I
    > could always use the C++ compiler to get it work.


    So, because performance is important, you are writing lots of unit tests,
    and they all time the code, right?

    > My concerns are:
    >
    > 1) Would the way I mix C and C++ code have any potential drawbacks to
    > the performance?


    Implementation-specifically, the two might have different calling
    conventions. These should be so close to the noise you shouldn't care.

    > 2) Would the way I mix C and C++ code have any potential drawbacks for
    > the future users to use the library?


    Yes.

    The general topic here is optimization. The most important resource to
    optimize is development time. You should write clean clear code that works,
    and only worry about optimization after measuring the results.

    So, you should give your future users clean code, without extra gizmos, like
    C calls, under the assumption they perform better.

    > My intention for choosing C interface instead of C++ OOD is to gain the
    > maximum performance as possible, yet I still like to use some C++
    > coding features (e.g., "const", reference instead of pointers, ...).


    This is the common FAQ "C is faster than C++, right?" The answer is C++ lets
    ambitious programmers write much more code, faster, than C. Hence, much
    library code is often slow. However, C++ still always leaves the programmer
    in control of speed. Don't use such libraries.

    http://www.parashift.com/c -faq-lite/mixing-c-and-cpp.html#faq-32.10

    <<One of C's great strengths is the fact that it has "no hidden mechanism":
    what you see is what you get. You can read a C program and "see" every clock
    cycle. This is not the case in C++; old line C programmers (such as many of
    us once were) are often ambivalent (can you say, "hostile"?) about this
    feature. However after they've made the transition to OO thinking, they
    often realize that although C++ hides some mechanism from the programmer, it
    also provides a level of abstraction and economy of expression which lowers
    maintenance costs without destroying run-time performance. >>

    In the case of an algorithmic library, you should research "template
    metaprogramming". That's a trick to push lots of computation back to compile
    time, not run-time. So the resulting program can out-perform carefully tuned
    (but manually written) C code.

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
     
    Phlip, Jul 29, 2006
    #2
    1. Advertising

  3. ziman137 wrote:
    > I am currently working to implement an algorithmic library. Because
    > the performance is the most important factor in later applications, I
    > decide to write it in C instead of C++.


    To be honest with you, I don't see any, yes, *any*, reason for that.

    > However, I thought it might
    > be convenient to use some C++ code at some misc places. I'm aware
    > that, I could always use the C++ compiler to get it work.
    >
    > My concerns are:
    >
    > 1) Would the way I mix C and C++ code have any potential drawbacks to
    > the performance?


    Usually there is no problem when mixing C and C++. But what is "the way"
    you "mix C and C++" that you're referring to?

    > 2) Would the way I mix C and C++ code have any potential drawbacks for
    > the future users to use the library?


    I have no idea how to answer. What "way" is that? How *do* you mix
    C and C++ code? Perhaps if you explain, any drawbacks could be found;
    but generally speaking, at this time of language development, there is
    no foreseeable drawbacks for mixing the two languages.

    > My intention for choosing C interface instead of C++ OOD is to gain
    > the maximum performance as possible, yet I still like to use some C++
    > coding features (e.g., "const", reference instead of pointers, ...).


    I can see that you yourself are struggling with your reasoning behind
    choosing to mix C and C++ in the implemenation. Why don't you simply
    use C++ for everything except the interface. Noting precludes you from
    picking 'extern "C"' for all functions in the API.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 29, 2006
    #3
  4. ziman137

    Ian Collins Guest

    ziman137 wrote:
    > Hello all,
    >
    > I have a question and am seeking for some advice.
    >

    Please don't multi-post to clc and clc++, if you think your question is
    relevant to both groups, cross-post.

    --
    Ian Collins.
     
    Ian Collins, Jul 29, 2006
    #4
  5. ziman137

    Cy Edmunds Guest

    "ziman137" <> wrote in message
    news:...
    > Hello all,
    >
    > I have a question and am seeking for some advice.
    >
    > I am currently working to implement an algorithmic library. Because
    > the performance is the most important factor in later applications, I
    > decide to write it in C instead of C++. However, I thought it might be
    > convenient to use some C++ code at some misc places. I'm aware that, I
    > could always use the C++ compiler to get it work.
    >
    > My concerns are:
    >
    > 1) Would the way I mix C and C++ code have any potential drawbacks to
    > the performance?
    > 2) Would the way I mix C and C++ code have any potential drawbacks for
    > the future users to use the library?
    >
    > My intention for choosing C interface instead of C++ OOD is to gain the
    > maximum performance as possible, yet I still like to use some C++
    > coding features (e.g., "const", reference instead of pointers, ...).
    >
    > Thanks,
    >
    > Gary
    >


    I often write my libraries with a pure C interface because so many scripting
    languages can use that directly. Also, some of the "C++ coding features"
    don't work so well at the interface. For instance, name mangling can make
    your shared library incompatible with a calling program written using a
    different compiler. C interfaces do not have this problem.

    Aside from these considerations I think there is really no reason to use C.
    One of Stroustrop's objectives for C++ was to make it a better C. Do you
    have some reason to think he failed?

    Cy
     
    Cy Edmunds, Jul 29, 2006
    #5
  6. ziman137 posted:

    > I have a question and am seeking for some advice.



    You only use "for" with "looking", e.g.: looking for some advice, seeking
    some advice.


    > I am currently working to implement an algorithmic library. Because
    > the performance is the most important factor in later applications, I
    > decide to write it in C instead of C++. However, I thought it might be
    > convenient to use some C++ code at some misc places. I'm aware that, I
    > could always use the C++ compiler to get it work.



    If don't see any reason why the C code would potentially have greater
    performance. I myself am a C++ programmer, but my code has a heavy bias
    toward C. Even if I write C-style code, it's always C++, and I always
    compile it as C++, because I invariably use C++ features here and there.


    > 1) Would the way I mix C and C++ code have any potential drawbacks to
    > the performance?



    I doubt it.


    > 2) Would the way I mix C and C++ code have any potential drawbacks for
    > the future users to use the library?



    Well you're using two languages instead of one -- it adds complication.


    > My intention for choosing C interface instead of C++ OOD is to gain the
    > maximum performance as possible, yet I still like to use some C++
    > coding features (e.g., "const", reference instead of pointers, ...).



    C has const.

    Why not write C++ code in the style of C, and compile it with a C++
    compiler? I don't see any reason why it should be any less efficient than
    corresponding C code.

    --

    Frederick Gotham
     
    Frederick Gotham, Jul 29, 2006
    #6
  7. ziman137

    Jerry Coffin Guest

    In article <>,
    says...
    > Hello all,
    >
    > I have a question and am seeking for some advice.
    >
    > I am currently working to implement an algorithmic library. Because
    > the performance is the most important factor in later applications, I
    > decide to write it in C instead of C++.


    A mistake based on a serious and fundamental misunderstanding. There
    are reasons to use C instead of C++, but performance is virtually
    never one of them. C outperformed C++ with a few early compilers, but
    with almost any reasonably modern compiler, C++ should consistently
    perform at least as well as equivalent C -- and C++ provides features
    that often allow it to produce far _better_ performance.

    > However, I thought it might be
    > convenient to use some C++ code at some misc places. I'm aware that, I
    > could always use the C++ compiler to get it work.


    Then use C++ and don't worry about it.

    > 1) Would the way I mix C and C++ code have any potential drawbacks to
    > the performance?


    Since you haven't really told any details about how you're mixing the
    two, it's hard to answer that. The mere fact that you're mixing the
    two doesn't imply any loss of performance compared to pure C -- but
    unless you're very careful, it might cause you to overlook some
    chances that C++ would have given you for improving the performance.

    > 2) Would the way I mix C and C++ code have any potential drawbacks for
    > the future users to use the library?


    Again, since you haven't told how you're doing it, it's hard to say.
    That said, chances are it's going to be a pain for them. On one hand,
    using C++ rules out their being able to use it (at all) from a pure C
    environment. At the same time, the fact that you're primarily using a
    C-style interface rules out gaining most of the possible benefits of
    using C++.

    All in all, my immediate reaction is that you've picked nearly the
    worst of all possible worlds: you're giving up most of the good
    points of C++, but gaining none of the good points of a pure C
    interface either.

    > My intention for choosing C interface instead of C++ OOD is to gain the
    > maximum performance as possible, yet I still like to use some C++
    > coding features (e.g., "const", reference instead of pointers, ...).


    My advice would be to pick one or the other. There are a number of
    valid reasons for picking C over C++ (e.g. compatibility) but
    performance is NOT among them. If one of those reasons applies,
    however, you need to really and truly use pure C and compile it with
    a C compiler. Even if you write code that a C compiler could digest,
    an object file produced by a C++ compiler won't give you the benefits
    of C.

    If you're going to use C++, then use it as it's really intended to be
    used. Templates (for only one example) often provide opportunities
    for improving not only versatility, but also performance. One of the
    basic intents of C++ was to make it easier to write good libraries,
    and (particularly) to make it easier to use those libraries. Using a
    C-like interface gives up the majority of that.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jul 29, 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. Kenneth Myhra
    Replies:
    2
    Views:
    435
    Kenneth Myhra
    Feb 16, 2004
  2. David Wilding
    Replies:
    6
    Views:
    424
    Whitecrest
    Apr 3, 2004
  3. Neil Zanella
    Replies:
    49
    Views:
    1,059
    Flash Gordon
    Feb 17, 2004
  4. Replies:
    1
    Views:
    399
  5. ziman137

    A concern about mixing C and C++

    ziman137, Jul 29, 2006, in forum: C Programming
    Replies:
    28
    Views:
    761
    Dave Thompson
    Aug 14, 2006
Loading...

Share This Page