473,785 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A concern about mixing C and C++

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

Jul 29 '06 #1
28 3102
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.
Jul 29 '06 #2
On 28 Jul 2006 22:28:52 -0700, "ziman137" <ga********@yah oo.comwrote
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 29 '06 #3

"ziman137" <ga********@yah oo.comwrote 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.
Jul 29 '06 #4
Malcolm wrote:
"ziman137" <ga********@yah oo.comwrote 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.
Jul 29 '06 #5
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++.
Jul 29 '06 #6
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)
Jul 29 '06 #7

"Ian Collins" <ia******@hotma il.comwrote in message
news:4j******** *****@individua l.net...
Malcolm wrote:
>"ziman137" <ga********@yah oo.comwrote 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.
..
Jul 29 '06 #8

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 "algorithmi c" 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.

Jul 29 '06 #9
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
Jul 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3483
by: bergel | last post by:
Hello, Does anyone already have some experience in mixing AWT and Swing? Is it conceptually doable? Does the design of Swing prevent interaction between an AWT and a Swing widget? Regards, Alexandre
6
4909
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive user might expect because the file iterator buffered data and readline did not read from that buffer. Hence the call to readline might unexpectedly skip some lines.
0
1829
by: Erik Max Francis | last post by:
Is there any prohibition against mixing different protocols within the same pickle? I don't see anything about this in the Python Library Reference and, after all, the pickle.dump function takes a protocol argument for each time it's called. (This is in Python 2.3.3.) I have a pickle containing two objects: a tag string and a (large) object containing many children. The identifying string is there so that you can unpickle it and...
4
23691
by: Rudolf | last post by:
Is it possible to add a vb.net source code module to a c# project and if so how? Thanks Rudolf
1
3419
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I would like to open C++ file streams due to easier exception handeling and safe closure of file ressources. Question 1: I open a standard file stream and want to transfer some binary read bits
4
1938
by: Cristian Tota | last post by:
Hi, I'd appreciate any thoughts on mixing C++ and C code. I have a project that uses a given C interface, the rest of the project can be either in C or C++. What would be the recomended design pattern in this case? C++ allows for a better design, but integrating the code with the C code isn't straight forward, it becomes rather messy. Code readability and maintenance are important, since the project will grow in the future. Maybe someone...
49
5908
by: Neil Zanella | last post by:
Hello, Often I happen to be dealing with nonnegative integers and since I know I won't need negative numbers here I declare them as unsigned simply to make the program somewhat clearer. Effectively, though, signed integers would often work too since unless I am doing some modular arithmetic modulo the word length then I almost never need to use the high bit since the integers I deal with are usually not that large, and I would assume...
2
2420
by: Dan | last post by:
Hi What are the dangers of mixing asp and asp.net? For the .net part of the site i will need to use the global.asax file but for the asp parts it will be using the other global. file, is there anyway to get to both use the same one? For example i want to convert my homepage to aspx first, but in doing so my session var that needs to be initialised wont be as it will use the global.asax and not the global.asa.
6
301
by: ziman137 | last post by:
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.
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.