473,807 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it a good thing that program mix C and C++?

Is it a good thing that program mix C and C++?

Mar 28 '07
45 2079
On Mar 28, 1:39 pm, "Sheth Raxit" <raxitsheth2... @yahoo.co.inwro te:
On Mar 28, 11:33 am, CBFalconer <cbfalco...@yah oo.comwrote:
dolphin wrote:
Is it a good thing that program mix C and C++?
Mix, no. You can call C routines from C++, but not the reverse.

one can call c from c++, and viceversa.
i was 'technically' wrong, but i think effect is almost same.
>

--
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 fromhttp://www.teranews.co m- Hide quoted text -

- Show quoted text -

Mar 29 '07 #31

Kenneth Brody <ke******@spamc op.netwrote in message
news:46******** *******@spamcop .net...
Kenny McCormack wrote:
In article <sD************ ********@bgtnsc 04-news.ops.worldn et.att.net>,
Bill Reid <ho********@hap pyhealthy.netwr ote:
...
>object files pretty much at will. It is quite obvious that some people
>here who are experts on the return value of main() don't understand
>this.
Well said, sir! And, you might add, the ethics of casting the return
value of malloc().

Note (for the uninitiated): The "casting the return value of malloc()"
thing is an idea that is taken as gospel around here, but has no real
world traction outside of this NG. It is certainly possible to disagree
with this bit of dogma, but doing so will win you no friends around
here.
>
It's considered bad practice, as it can hide a missing #include for
the malloc-and-friends prototypes. Without those prototypes, all
bets are off as to what happens when the compiler takes what it
thinks is an int return and casts it into your pointer.
Yeah, yeah, yeah, but interestingly apropos to the original question,
if you should for some reason decide to use malloc() instead of new()
in a C++ program, you MUST cast the return value. If you don't,
you'll get a compiler error! (Which happens for many of the "little"
differences between C types, standard libraries, and semantics
when used in a C++ program.)

And of course, it is exactly this required explicit typing of return
values in C++ that precludes all the "horrible" problems (which
only occur if you make another ACTUAL mistake) of NOT casting
the value in C!

But like the use of extern "C", this is not just off-topic, but beyond
the scope of knowledge of many of the frequent posters here, as
evidenced by their struggle in this thread to come to grips with
some simple realities of real-world programming as it is and was
performed at any time in the last 15 years or so...

---
William Ernest Reid

Mar 29 '07 #32
CBFalconer <cb********@yah oo.comwrote:
Ian Collins wrote:
CBFalconer wrote:
Ian Collins wrote:

No so, C++ provides a mechanism (the extern "C" linkage specifier)
to make C++ functions callable from C.

That makes C callable from C++. Not the reverse.
It's a two way street.
No it isn't. The C++ compiler does various unspeakable things to
the function names [ ... ]
The <<extern "C">constru ct of C++ allows the programmer to
write C++ functions that are callable from C. That's a fact.

You can also write C++ functions that are not callable from C. So
what. To use such functions in a C program you have to write a
wrapper, necessarily in C++, that *is* callable from C.

It's not just a name mangling issue, the ABI's can be different
and that comes up when passing a C++ function pointer to a C library
that wants a callback.

To the OP: you can mix your C and C++ exactly as you would mix C
and, say, Fortran. Treat them as distinct languages and do what
you have to do to create working interlanguage bindings.
--
pa at panix dot com
Mar 29 '07 #33
Bill Reid wrote:
>
.... snip ...
>
Yeah, yeah, yeah, but interestingly apropos to the original question,
if you should for some reason decide to use malloc() instead of new()
in a C++ program, you MUST cast the return value. If you don't,
you'll get a compiler error! (Which happens for many of the "little"
differences between C types, standard libraries, and semantics
when used in a C++ program.)
Did you by any chance ever note the name of this newsgroup? If so,
will you kindly explain to me (and others) where in it the
character sequence "++" appears?

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

Mar 30 '07 #34

CBFalconer <cb********@yah oo.comwrote in message
news:46******** *******@yahoo.c om...
Bill Reid wrote:
... snip ...

Yeah, yeah, yeah, but interestingly apropos to the original question,
if you should for some reason decide to use malloc() instead of new()
in a C++ program, you MUST cast the return value. If you don't,
you'll get a compiler error! (Which happens for many of the "little"
differences between C types, standard libraries, and semantics
when used in a C++ program.)

Did you by any chance ever note the name of this newsgroup? If so,
will you kindly explain to me (and others) where in it the
character sequence "++" appears?
Nowhere! Which is why I am concerned about the topicality of
this previous post in this thread:

Date: Wed, 28 Mar 2007
From: CBFalconer <cb********@yah oo.com>
Newsgroups: comp.lang.c
Subject: Re: Is it a good thing that program mix C and C++?

dolphin wrote:
>
Is it a good thing that program mix C and C++?
Mix, no. You can call C routines from C++, but not the reverse.

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

---end of archived post

Actually, I'm not that concerned about the dreaded "++" as much
as the fact that it was just technically incorrect. Bad information is
ALWAYS off-topic in any group AFAIC...

So my advice is to stick with what you know. If you don't know
much, don't post much, or honestly ask for help from people who
do know. Simple enough?

---
William Ernest Reid

Mar 30 '07 #35
In article <Y0************ *********@bgtns c04-news.ops.worldn et.att.net>,
Bill Reid <ho********@hap pyhealthy.netwr ote:
(poor, misunderstood CBF wrote)
>Did you by any chance ever note the name of this newsgroup? If so,
will you kindly explain to me (and others) where in it the
character sequence "++" appears?
Nowhere! Which is why I am concerned about the topicality of
this previous post in this thread:

Date: Wed, 28 Mar 2007
From: CBFalconer <cb********@yah oo.com>
Newsgroups: comp.lang.c
Subject: Re: Is it a good thing that program mix C and C++?

dolphin wrote:
>>
Is it a good thing that program mix C and C++?
(And, again, poor, misunderstood CBF wrote)
>Mix, no. You can call C routines from C++, but not the reverse.
See, that's really the problem with being so anal about the topicality
nonsense. You can't help but eventually become hoist on your own petard.

But, of course, the regs get around this by implicitly (and from time to
time, explicitly, as in the recent fracas where good ole Heathfield
started posting a bunch of wildly OT crud) having different rules for
the regs than for the hoi polloi.
>Actually, I'm not that concerned about the dreaded "++" as much
as the fact that it was just technically incorrect. Bad information is
ALWAYS off-topic in any group AFAIC...

So my advice is to stick with what you know. If you don't know
much, don't post much, or honestly ask for help from people who
do know. Simple enough?
Good advice. But following it would mean the death of this (very
entertaining) newsgroup. We can't have that.

Mar 30 '07 #36

"Kenny McCormack" <ga*****@xmissi on.xmission.com wrote in message
news:eu******** **@news.xmissio n.com...
In article <sD************ ********@bgtnsc 04-news.ops.worldn et.att.net>,
Bill Reid <ho********@hap pyhealthy.netwr ote:
...
>>object files pretty much at will. It is quite obvious that some people
here who are experts on the return value of main() don't understand
this.

Well said, sir! And, you might add, the ethics of casting the return
value of malloc().

Note (for the uninitiated): The "casting the return value of malloc()"
thing is an idea that is taken as gospel around here, but has no real
world traction outside of this NG. It is certainly possible to disagree
with this bit of dogma, but doing so will win you no friends around here.
I was converted to uncast malloc() by the ng. Not for the reason given -
that it can mask failure to include stdlib.h. In production code that just
can't happen. My reason was to reduce visual clutter, and because a C
function only rarely needs to be cut and pasted into C++.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 30 '07 #37
In article <Rs************ *************** ***@bt.com>,
Malcolm McLean <re*******@btin ternet.comwrote :
....
>I was converted to uncast malloc() by the ng. Not for the reason given -
that it can mask failure to include stdlib.h. In production code that just
can't happen. My reason was to reduce visual clutter, and because a C
function only rarely needs to be cut and pasted into C++.
Agreed on all counts. The "mask failure" arguement is particularly
weak. It is very similar to the "if (1 == a) rather than if (a == 1)"
thing, where, this may have had some traction at some point in the deep
dark past, but nowadays any compiler worth 2 bits will catch it (i.e.,
warn about it) anyway.

The point is that is you consider the following three commonly harped on
things in this NG:

1) char a[10]; x = a[10];
2) void main()
3) char *x = (char *) malloc(10);

The first is clearly an error. The 2nd probably, and the third, really not.
Yet all three are harped upon with equal vigor, by a bunch of freaks who
don't understand the concept of degree.

Mar 31 '07 #38
On Fri, 30 Mar 2007 23:37:26 +0100, Malcolm McLean wrote:
"Kenny McCormack" <ga*****@xmissi on.xmission.com wrote in message
news:eu******** **@news.xmissio n.com...
>In article <sD************ ********@bgtnsc 04-news.ops.worldn et.att.net>,
Bill Reid <ho********@hap pyhealthy.netwr ote: ...
>>>object files pretty much at will. It is quite obvious that some people
here who are experts on the return value of main() don't understand
this.

Well said, sir! And, you might add, the ethics of casting the return
value of malloc().

Note (for the uninitiated): The "casting the return value of malloc()"
thing is an idea that is taken as gospel around here, but has no real
world traction outside of this NG. It is certainly possible to
disagree with this bit of dogma, but doing so will win you no friends
around here.
I was converted to uncast malloc() by the ng. Not for the reason given -
that it can mask failure to include stdlib.h. In production code that
just can't happen.
I have corrected a live, delivered, customer-affecting bug in 'production
code' caused by failure to include stdlib.h, masked by casting the return
from malloc. "Just can't happen" is thereby proven to be something of an
exaggeration. To the (reasonable) argument "but it must have been
seriously inexperienced programmers", I point out that's _exactly_
the audience for "Don't Cast Malloc()".

As for C++, should we not be recommending <OTuse new() <OT>?

Martin
--
Martin Golding | He who steals my code steals trash.
DoD #0236 | (Twas mine, tis his, and will be slave to thousands.)
A poor old decrepit Pick programmer. Sympathize at:
fo*****@comcast .net Vancouver, WA
Mar 31 '07 #39
Malcolm McLean said:
I was converted to uncast malloc() by the ng. Not for the reason given
- that it can mask failure to include stdlib.h. In production code
that just can't happen.
When "just can't happen" relies on a basic level of human competence, we
can count on it happening at least 17 times a day, 260 days a year.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 31 '07 #40

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

Similar topics

24
3619
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works, first and foremost. If it works well, even better. The same goes for ease of maintenance, memory footprint, speed, etc, etc. Most of the time, people are writing code for a use in the *real world*, and not just as an academic exercise. Look at...
36
4658
by: toedipper | last post by:
Hello, I am designing a table of vehicle types, nothing special, just a list of unique vehicle types such as truck, lorry, bike, motor bike, plane, tractor etc etc For the table design I am proposing a single column table with a field name called vehicle_type and this will contain the vehicle type. Sot it will be
39
2869
by: Suresh | last post by:
Hi, I am new to C and curious to know some basic questions about C. Why C is good? In what way its better than any other languages? If it is no longer a choice of programming language...why people still study it? And also its not an OO language either...is there any advantages in being not an OO language? I know one reason is that, IT started with C and so they are still using
113
12362
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same algorithm work with strings that may or may not be unicode 3) Number of bytes back must either be <= number of _TCHARs in * sizeof(_TCHAR), or the relation between output size and input size can be calculated simply. Has to take into account the...
13
2105
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My example is a program called HijackThis. I'm sure many are familiar that it is a spyware removal tool. The program looks at over 20 places on Windows computers to see what is starting (with options to remove the offending software). The program can...
11
3811
by: Someonekicked | last post by:
I want to save tables of integers to files. One way to write the cells as fixed size in the file, is to use reinterpret_cast. Is that a bad choice, good choice? I remember once before I posted a program using it, and some suggested that I might get errors using it. are there any other ways to write the cells as fixed size in the file? (knowing the cells will only contain integers). here is how I plan to do it (without much caring...
63
3991
by: John Salerno | last post by:
I know there's a request for a good IDE at least once a week on the ng, but hopefully this question is a little different. I'm looking for suggestions for a good cross-platform text editor (which the features for coding, such as syntax highlighting, etc.) but not a full IDE with all the fancy jazz (GUI developer, UML diagrams, etc.). Ideally, it would be something I could even put on a flash drive and move from computer to computer, but...
6
1875
by: mast2as | last post by:
I have posted a few messages in the last few days about a project I am working on which is a quite simple parser. I ended up using the try/ catch structure as a general mechanism to control what's happening in the program when it finds a syntax error in the file it parses. I would like to know if this is 'an acceptable thing to do' from a programmation point of view, or if I should keep using it only for catch exception created withing the...
0
9720
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
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
10626
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...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10112
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...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
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...
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.