Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 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
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-29-2006
ziman137 wrote:
> 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++.


Why? The performance of a C++ version should be no worse than the C one
and possibly better if you can make use of existing well tuned standard
algorithms and containers. What did your profiling show?

> 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.
>

You could, yes.

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


None what so ever.

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

Yes, unless you release the source, if you expose any C++ interfaces you
will run into all of the ABI incompatibility problems that plague C++
libraries. You will also exclude C users from using you library. One
compromise is to only expose a C interface.

> 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, ...).
>

Performance has nothing to do with it, portability is the reason to
stick with a C interface.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      07-29-2006
On 28 Jul 2006 22:28:52 -0700, "ziman137" <> wrote
in comp.lang.c:

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


Here's advice: don't ask here. As far as C is concerned, C++ is just
one of many, many johnny-come-lately imitators that claim to improve
and extend C.

The C standard does not define an interface to any other language. Not
at all. C++, on the other hand, defines a mechanism that is intended
(but not guaranteed) to allow mixing code from a compatible C
compiler.

> 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.


Absolutely astonishing. What proof do you have that C will provide
better performance than C++? What trials have you run, and where are
the timing comparison results?

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


C does not define any way to mix C and C++.

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


C does not define any way to mix C and C++.

> 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, ...).


But what evidence do you have that this choice will have any impact on
performance?

As for mixing C++ coding features such as "references" with C code,
that's just plain impossible, since there are no such things as
references in C.

Here are some hints:

If you want to know about mixing C++ and C, ask in a C++ newsgroup.

If you want to know about mixing Java and C, ask in a Java group.

If you want to know about...

....well, you're probably getting the point by now.

C is far senior to many of the popular flavor of the month languages,
and does not take any notice of their existence. And it may well
outlast many or all of them, as it has outlasted at least the original
Visual Basic for Windows.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      07-29-2006



"ziman137" <> wrote in message
> 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.
>
> 1) Would the way I mix C and C++ code have any potential drawbacks to
> the performance?
>

Potentially yes. It must be compiled with a C++ compiler, and it is not
impossible that the C++ compiler on a given system is inferior to the C
compiler. However it is not very likely, often they share substantially the
same code.
If you use C++ constructs in your code you will find them harder to optimise
than the C constructs, because C++ is designed to present a nice interface
to the programmer rather than to expose basic processor operations. However
you are probably aware of that already.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-29-2006
Malcolm wrote:
> "ziman137" <> wrote in message
>
>>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.
>>
>>1) Would the way I mix C and C++ code have any potential drawbacks to
>>the performance?
>>

>
> Potentially yes. It must be compiled with a C++ compiler, and it is not
> impossible that the C++ compiler on a given system is inferior to the C
> compiler. However it is not very likely, often they share substantially the
> same code.
> If you use C++ constructs in your code you will find them harder to optimise
> than the C constructs, because C++ is designed to present a nice interface
> to the programmer rather than to expose basic processor operations. However
> you are probably aware of that already.


Eh? C++ can wrap basic processor operations in a higher level
abstraction, but it can still expose then in the same way C does.

--
Ian Collins.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      07-29-2006
Jack Klein a écrit :
>
> Absolutely astonishing. What proof do you have that C will provide
> better performance than C++? What trials have you run, and where are
> the timing comparison results?
>


We discussed this with an example on Saturday May 6th.
The topic of the thread was

strtok behavior with multiple consecutive delimiters

and C++ came at best as twice as slow as C.

jacob

P.S. this is NOT a "proof" of course. There may be situations
where C++ templates outperform assembly language, and there will
be situations when lisp outperforms C and C++.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-29-2006
jacob navia said:

> There may be situations
> where C++ templates outperform assembly language,


Jacob's talking through his hat again. His claim is easily disproved. All we
have to do is tell the compiler to generate assembly language from the C++
template code. We now have assembly language that performs exactly as well
as the C++ template code. And, given an experienced assembly language
programmer (to match the experienced C++ programmer who produces such
astoundingly quick code), we can almost certainly find an optimisation,
however trivial, that will make the assembly language version at least a
little faster than the C++ template version from which it was generated.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      07-29-2006



"Ian Collins" <ian-> wrote in message
news:...
> Malcolm wrote:
>> "ziman137" <> wrote in message
>>
>>>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.
>>>
>>>1) Would the way I mix C and C++ code have any potential drawbacks to
>>>the performance?
>>>

>>
>> Potentially yes. It must be compiled with a C++ compiler, and it is not
>> impossible that the C++ compiler on a given system is inferior to the C
>> compiler. However it is not very likely, often they share substantially
>> the
>> same code.
>> If you use C++ constructs in your code you will find them harder to
>> optimise
>> than the C constructs, because C++ is designed to present a nice
>> interface
>> to the programmer rather than to expose basic processor operations.
>> However
>> you are probably aware of that already.

>
> Eh? C++ can wrap basic processor operations in a higher level
> abstraction, but it can still expose then in the same way C does.
>

Can, yes. But Bjarne Strousup believes that malloc(), for example, should
not be used in new C++ code.
Modern C++ using the standard library will have set of abstracted interfaces
to basic structures. They work well and might even be more efficient than
hand-coded similar C structures. However it is not possible for the
programmer to exert fine control over them. So for instance if he wants to
interate over an array, he will use the vector class iterator. This may be a
bare pointer, but probably it carries some run time checks with it to
protect the pointer from going out of bounds. Generally a good thing, but
harder to optimise.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
..


 
Reply With Quote
 
chris.fairles@gmail.com
Guest
Posts: n/a
 
      07-29-2006

ziman137 wrote:
> 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


Hello Gary,

I've recently gone through the process of deciding to use C or C++ for
a high performace "algorithmic" library.

As far as raw performance goes, you'll find benchmarks throughout the
net going both ways. Since C++ is more popular, you'll see more claming
C++ is as fast/faster. At the end of the day it seems that if you just
want to generically compare performance of C vs C++ you'll arrive at
the answer "it depends on how well you know and understand the
language".

I'm much more familiar with C. I know how it works, I know how to make
nice API's and modular code with it and I know how to avoid some issues
that might degrade performance. But these will apply to both C++ and C
for the most part since C++ is a superset, things like use narrow
ranged case statements, avoid deeply nested if statements, using the
smallest integer types isnt always beneficial etc etc.

However, I don't know much about how templates are internally handled,
I don't know how multiple inheritance works and I have only a small
notion of how virtual functions work. So the problem for me is that I
don't know how to write efficient C++ code because I don't fully
understand how it all works. Not only that but because I've used C
since forever, I don't have much intuition when it comes to OO
application design. So I stick with C. Call me lazy

So my advice, either stick with what you know or learn the performance
pitfalls.

Cheers,
Chris

P.S. as for combining C++ and C in a scientific libray, check out
openCV. It's a computer vision API written like C but with both a C and
C++ interface.

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      07-29-2006
Malcolm posted:

> Modern C++ using the standard library will have set of abstracted
> interfaces to basic structures. They work well and might even be more
> efficient than hand-coded similar C structures. However it is not
> possible for the programmer to exert fine control over them. So for
> instance if he wants to interate over an array, he will use the vector
> class iterator. This may be a bare pointer, but probably it carries some
> run time checks with it to protect the pointer from going out of bounds.
> Generally a good thing, but harder to optimise.



There is nothing stopping a C++ programmer from simply using the C features,
i.e. normal arrays, rather than std::vector.

I myself write C++ code with a heavy bias toward C.

--

Frederick Gotham
 
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++ 6 07-29-2006 02:10 PM
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57