Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A concern about mixing C and C++

Reply
Thread Tools

A concern about mixing C and C++

 
 
ziman137
Guest
Posts: n/a
 
      07-29-2006
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

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      07-29-2006
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-lit...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!!!


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-29-2006
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


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-29-2006
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.
 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      07-29-2006

"ziman137" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      07-29-2006
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
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      07-29-2006
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
design patterns concern; gui and business objects vangjake@googlemail.com Java 1 11-19-2006 02:02 AM
A concern about mixing C and C++ ziman137 C Programming 28 08-14-2006 06:59 AM
efficiency concern: when to really use unsigned ints and when not to Neil Zanella C Programming 49 02-17-2004 05:13 PM
Bug or security concern related to upload of binary files and IHttpModule? Kenneth Myhra ASP .Net 2 02-16-2004 01:43 PM
Cisco IPSEC VPN to CheckPoint firewall and linux server concern qazaka Cisco 0 10-09-2003 08:18 AM



Advertisments