Thread: Tips for converting C prog. to C++?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    35

    Tips for converting C prog. to C++?

    Hi there,
    I am keen to get my C program to compile under C++ because then I can experiment with using some C++ features to optimise the code (and yes unfortunately I do need to optimise, it's a scientific program which does HEAPS of calculations inside the inner loop) and I'd quite like to be able to use inline and some class features. (and yes I know C99 supports inline, but I really don't like the idea that declaring something inline is no guarantee that it WILL be inlined.)

    Are there any tips that people experienced in both languages might have for converting from C to C++? any gotchas that I must look out for?

    many thanks
    VIM + gcc + beer... does life get any better?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > but I really don't like the idea that declaring something inline is no guarantee that it WILL be inlined
    C++ makes no such guarantee either as far as I know.

    After all, a loop calling a function which fits entirely in cache would seem to be better than an unrolled loop which doesn't. Look-ahead and branch prediction in modern processors make things like function calls almost free, especially if you're calling a leaf function (one which doesn't call other functions).

    > any gotchas that I must look out for?
    Yeah, compilers are usually better at optimising than you are.
    Write clear code and profile it before trying to work out how to make it better.
    Now off to read your other thread....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    but I really don't like the idea that declaring something inline is no guarantee that it WILL be inlined.
    If I remember correctly, that applies to C++ as well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    thanks for telling me that!
    C++ has suddenly lost a lot of its appeal (for this task anyway).

    Odd thing is, I tried compiling with -O2 instead of -O3 and I got a 'variable might be used uninitialized in this function' error that I didn't get with -O3.

    is that normal? (ie for stuff that should cause warnings based on the warning flags you've used to not generate warnings as a result of some other flags)?

    seems very strange to me.
    VIM + gcc + beer... does life get any better?

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    ok this is *very* scary.

    with -O3:
    <Eloc> = -7.478(5)
    t = 114s

    with -O2:
    <Eloc> = -7.478(5)
    t = 113.8s

    with -O1:
    <Eloc> = -7.498(6)
    t = 108.9s


    where <Eloc> is the average value of the 'local energy' (of the Lithium trial wavefunction) and the number in parentheses signifies the error in the last number ( so -7.478(5) means that the value is -7.478 +/- 0.005 )...

    any ideas why I am getting a different answer when using -O1 ??

    Because this is a SIGNIFICANT difference for the purposes of this calculation.

    *edit*
    this is insanely bad.
    The result with no -O options at all is:

    <Eloc> = -7.472(5)
    t = 119.76s

    my question now is, what the hell am I able to trust??? why am I getting different numerical answers to this when I use different compiler options and what the hell am I supposed to do!? I don't know what to trust any more. I can't just pick one at random - either one is correct and the rest are wrong or they are all wrong, but which is which?!


    *edit

    is there some sort of unwritten law that I have not come across that says you shouldn't use any compiler options/optimisation for sensitive scientific calculations?

    I have already discarded some complex changes to my code that *seriously* sped it up, because they perturbed the result! and now I find that changing from -O2 to -O1 perturbs the result more than they did!
    Last edited by eccles; 01-15-2005 at 04:09 AM.
    VIM + gcc + beer... does life get any better?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Floating point is tricky stuff - http://cch.loria.fr/documentation/IE...M/goldberg.pdf

    Basically, there is a difference between mathematical and computational results
    a = b * c / d;
    Mathematically, there is no problem, but computationally (because all floats are approximations), you can get different answers depending on whether you do b*c first or c/d first.

    Now there are limits to what the compiler is allowed to do by way of rearranging expressions, but I'd have to dig around to find all the details.

    Here's a quote from the draft C99 standard
    [#14] EXAMPLE 5 Rearrangement for floating-point expressions
    is often restricted because of limitations in precision as
    well as range. The implementation cannot generally apply
    the mathematical associative rules for addition or
    multiplication, nor the distributive rule, because of
    roundoff error, even in the absence of overflow and
    underflow. Likewise, implementations cannot generally
    replace decimal constants in order to rearrange expressions.
    In the following fragment, rearrangements suggested by
    mathematical rules for real numbers are often not valid (see
    F.8).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Amateur Programmer
    Join Date
    Nov 2003
    Posts
    11
    Best way to change a c program to c++? Easy, change the extension to .cpp and compile it as C++

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    using some C++ features to optimise the code
    C++'ising your code would probably make it easier to maintain and debug (from a C++ programmer's viewpoint), but there's no guarantee you'll make it faster or even as fast.

    Converting to C++ is a good idea for maintainence/extensibility/debugging reasons, which may be important too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming Tips
    By webmaster in forum General Discussions
    Replies: 27
    Last Post: 10-29-2018, 12:49 PM
  2. Getting input from another prog?
    By Badman3k in forum C Programming
    Replies: 4
    Last Post: 11-11-2004, 02:58 AM
  3. C++ Programming Tips
    By gflores in forum C++ Programming
    Replies: 20
    Last Post: 09-14-2004, 07:53 PM
  4. an option at the end of this prog to ask if I want to run again
    By bandito9111 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 02:30 PM
  5. any useful tips to increase speed when parsing files
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2003, 05:52 PM