Topic: Programming in C, using a C++ routine...query


Author: eddykim@aludra.usc.edu (I. P. Freely)
Date: 19 Mar 1993 00:00:46 -0800
Raw View
Hi, this is my first post, so please forgive me if I'm asking something
stupid.
I'm working on a program for an operating systems class.  The program is
using a kernal made just for teaching how to use os concepts.  The problem
is that it is written in c, and I would like to use C++.  The section of
code that I need to write is a part of the kernal that schedules the processes.
The previous post with the "extern" suggestion seems to be only for regular
C++ procedures.  Would that work also for classes?  So if the kernal called
my functions (coded in C++), it would work?
Thanks

Eddy Kim




Author: mat@mole-end.matawan.nj.us
Date: Sat, 27 Feb 1993 00:10:25 GMT
Raw View
In article <9305515.29606@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:

> 3. Recompiling C code with a C++ compiler will NOT make it faster, unless
>    you happen to have a bad C compiler and a good C++ compiler.

In at least one particular case, this isn't true.  On the Intel 386/486
family, there are two `standard' call/return sequences, the C sequence
and the Pascal sequence.  The Pascal sequence is faster because the stack
adjustment on return is completed by the return statement itself.  It can't
be used with C because there is no guarantee that every function call will
be made by code which sees a prototype, even when the prototypes exist.  It
can be used by C++ (except for functions with the ellipsis) because C++ does
make that promise.

But when the function is compiled with   extern "C" , the C calling
sequence must be used.
--
 (This man's opinions are his own.)
 From mole-end    Mark Terribile

 mat@mole-end.matawan.nj.us, Somewhere in Matawan, NJ




Author: kanze@us-es.sel.de (James Kanze)
Date: 27 Feb 93 16:33:08
Raw View
In article <1993Feb27.001025.15572@mole-end.matawan.nj.us>
mat@mole-end.matawan.nj.us writes:

|> In article <9305515.29606@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:

|> > 3. Recompiling C code with a C++ compiler will NOT make it faster, unless
|> >    you happen to have a bad C compiler and a good C++ compiler.

|> In at least one particular case, this isn't true.  On the Intel 386/486
|> family, there are two `standard' call/return sequences, the C sequence
|> and the Pascal sequence.  The Pascal sequence is faster because the stack
|> adjustment on return is completed by the return statement itself.  It can't
|> be used with C because there is no guarantee that every function call will
|> be made by code which sees a prototype, even when the prototypes exist.  It
|> can be used by C++ (except for functions with the ellipsis) because C++ does
|> make that promise.

Zortech does this, too.

More generally, however, the big win should come when you actually
write C++ code, since the stricter scoping, etc. should reduce the
aliasing problem for the optimizer.  In fact, however, I've seen just
the opposite effect.  Most of the optimizers were created for C, and
don't take advantage of the extra information you give it in C++,
whereas there is some overhead for things like virtual functions, etc.
--
James Kanze                             email: kanze@us-es.sel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                   -- Beratung in industrieller Datenverarbeitung




Author: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
Date: 28 Feb 93 02:34:43 GMT
Raw View
mat@mole-end.matawan.nj.us writes:

>In article <9305515.29606@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
>
>> 3. Recompiling C code with a C++ compiler will NOT make it faster, unless
>>    you happen to have a bad C compiler and a good C++ compiler.
>
>In at least one particular case, this isn't true.  On the Intel 386/486
>family, there are two `standard' call/return sequences, the C sequence
>and the Pascal sequence.  The Pascal sequence is faster because the stack
>adjustment on return is completed by the return statement itself.  It can't
>be used with C because there is no guarantee that every function call will
>be made by code which sees a prototype, even when the prototypes exist.  It
>can be used by C++ (except for functions with the ellipsis) because C++ does
>make that promise.

It *can* be used with C, because
 (1) Strictly conforming programs must not call functions with arguments
            that (after standard conversions) don't match the function's
            argument types, otherwise the effect is undefined.
 (2) Strictly conforming programs must not call functions with the
     ellipsis without a prototype, otherwise the effect is undefined.

It isn't often used, because many not-strictly-conforming programs violate
(1) and (2).  But C compilers often offer a compiler option to allow this.

(Turbo C offered such an option, but never claimed ANSI-compliance when
 using this option - and indeed wasn't ANSI-complaint, because you needed
 to insert 'cdecl' in the definition of main() to get it to work.
 This was because the startup-code called main() using the C calling sequence.
 A compiler bug, IMHO, and an easy one to fix at that.)

--
Fergus Henderson             fjh@munta.cs.mu.OZ.AU
This .signature virus is a self-referential statement that is true - but
you will only be able to consistently believe it if you copy it to your own
.signature file!




Author: nage@ellis.uchicago.edu (Rufus Nagel)
Date: 18 Feb 93 16:57:23 GMT
Raw View
 How can a C programmer use a function written by c++. I am a C
programmer, with limited knowledge of c++, so please answer the question
with that in mind. I would appreciate responses by e-mail, as I rarely
read this newsgroup.
Rufus Nagel (r-nagel@uchicago.edu)
Thanks.





Author: cmain@giraffe.ru.ac.za (MR CRAIG MAIN)
Date: 23 Feb 93 12:59:02 GMT
Raw View
In article <1993Feb18.165723.17793@midway.uchicago.edu> nage@ellis.uchicago.edu (Rufus Nagel) writes:
>From: nage@ellis.uchicago.edu (Rufus Nagel)
>Subject: Programming in C, using a C++ routine...query
>Keywords: c, c++, functions, objects
>Date: Thu, 18 Feb 1993 16:57:23 GMT
> How can a C programmer use a function written by c++. I am a C
>programmer, with limited knowledge of c++, so please answer the question
>with that in mind. I would appreciate responses by e-mail, as I rarely
>read this newsgroup.
>Rufus Nagel (r-nagel@uchicago.edu)
>Thanks.
>
>
>
Don't be rediculous, use the C functions in a C++ programme, not the other
way around, rewrite the C programme, or better still, just recompile it with
a C++ compiler, and bang - You have a full C++ executable. (It's faster too).




Author: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
Date: 24 Feb 93 04:47:04 GMT
Raw View
cmain@giraffe.ru.ac.za (MR CRAIG MAIN) writes:

>Don't be rediculous, use the C functions in a C++ programme, not the other
>way around, rewrite the C programme, or better still, just recompile it with
>a C++ compiler, and bang - You have a full C++ executable. (It's faster too).

1. It is quite reasonable to want to use C++ functions in your C code,
   and C++ explicitly provides a method for doing this - declare your
   C++ functions as extern "C".

 extern "C" void f(int x) {
  // you can call this function from your C code,
  // but this is still a C++ function
  cout << x;
 }

2. If you think that converting from C to C++ is always just a matter
   of recompilation, then you have clearly never tried it.

3. Recompiling C code with a C++ compiler will NOT make it faster, unless
   you happen to have a bad C compiler and a good C++ compiler.

--
Fergus Henderson             fjh@munta.cs.mu.OZ.AU
This .signature virus is a self-referential statement that is true - but
you will only be able to consistently believe it if you copy it to your own
.signature file!