Slashdot Mirror


Ask Slashdot: Is C++ the Right Tool For This Project?

ranton writes: I am about to start a personal project which I believe should be done in C/C++. The main reasons I have for this are the needs to manage memory usage and disk access at a very granular level and a desire to be cross-platform. Performance is also important but I am unlikely to spend enough time optimizing to be much faster than core libraries of higher level languages.

On the other hand, network access is also a critical part of the project and I am worried about the effort it takes to make cross platform code for both network and disk access. I have been working in the Java / C# world for the past decade and things like TCP/IP and SSL have just been done for me by core libraries. Do libraries like Boost or Asio do a good job of abstracting these aspects away? Or are there other options for doing granular memory and disk management with more high level languages that have better cross-platform library support? I am willing to brush up on my C/C++ skills if necessary but want to spend as much time as possible developing the unique and potentially innovative parts of my project. Thanks for any advice you can provide.

15 of 296 comments (clear)

  1. Plain ol' C might a better option by Damouze · · Score: 1, Insightful

    Depending on how hard-core object-oriented you wish your program to be, plain old C might be a much better option, especially if you everything can just be command-line only and will not need a GUI.

    Otherwise, and I'm almost loathe to mention these, C# and Java might be even better ways to go.

    C++ is needlessly complicated.

    --
    And on the Eighth Day, Man created God.
    1. Re:Plain ol' C might a better option by Anonymous Coward · · Score: 2, Insightful

      You can treat C++ as the name implies; plain old C with some extra stuff you may or may not use.
      Nothing in C++ is forcing you to create object oriented code.

  2. Re:If you cannot answer your own question.. by mwvdlee · · Score: 4, Insightful

    it seems to me like you need to look more closely to what you mean/need by 'granularity' and perhaps change your mentality using familiar languages, and the solutions for problems in those areas.

    This.

    "Very high" is subjective. I've had programming tasks where every single byte of memory counted and was optimized for. But I've also had tasks where this meant I'd just have to keep memory usage down to a few MB; totally different interpretations of "very high granularity".

    Since you'll probably need to have a trade-off between memory and performance, which is more important? Can you spare a few KB to make your code run 2x faster? How about a few MB? Does code need to run as fast as possible or just fast enough to keep pace with the GUI?

    --
    Slashdot social media options: AIM, ICQ, Yahoo, Jabber and Mobile Text. Why no MySpace?
  3. Re:python with psutil by The_Dougster · · Score: 4, Insightful

    Python can sure get you up and running like yesterday with a quick and dirty prototype. Bang it out in python and then port it to C++ at your leisure, if you even need to.

    Having a working solution *right now* is pretty nice, even if your ultimate goal is a C++ binary.

    --
    Clickety Click ...
  4. Re: C++ is never the right tool by loonycyborg · · Score: 5, Insightful

    I think that using smart pointers and RAII pattern is in all respects better than garbage collection.

  5. Why? by DrVxD · · Score: 3, Insightful

    The main reasons I have for this are the needs to manage memory usage and disk access at a very granular level

    And why, exactly, do you imagine you need these things?
    (You may well do - but you don't give a reason for it, so it's entirely possible that you don't need to manage those things on a granular level)

    --
    Not everything that can be measured matters; Not everything that matters can be measured.
  6. Re:There is no perfect lang by DrVxD · · Score: 5, Insightful

    C++ is often unreadable

    That's not a problem with the language, it's a problem with whoever wrote the code.
    I've been writing software for about 40 years - and one of the things I've observed in that time is that it's possible to write unreadable code in pretty much any language. I've also observed that it's possible to write readable code in pretty much any language.

    --
    Not everything that can be measured matters; Not everything that matters can be measured.
  7. Use one language by jones_supa · · Score: 4, Insightful

    I am about to start a personal project which I believe should be done in C/C++.

    I cringe when someone says "C/C++". Sort that out first by choosing one language for your project. Either write lean and clean pure C code, or fully use the proper abstractions of C++ to write memory-safe and easily-maintainable code, but don't make an unprofessional crusty mix of the languages.

  8. Re:C++ is never the right tool by Anonymous Coward · · Score: 2, Insightful

    Nothing important in VS is written in C#. The compiler, linker and so on - you know the things that need to be tight with optimal performance, are all written in C++. The platform itself (WPF) is written in C++ (Visual Studio UI is WPF). .NET CLR is written in C++. Do you want me to continue or are you happy enough stewing in your own ignorance?

  9. Re:C++ is never the right tool by rippeltippel · · Score: 4, Insightful

    Could you describe a project for which the choice of c++ is a good one?

    Guess what, you can easily do that:

    • 1) List ALL the software you normally use (don't forget your OS and the browser you're using now)
    • 2) Cross out the software written in C++
    • 3) Cross out the software written in a language which is itself written in C++ (e.g. Python)
    • 4) Cross out the software that rely on another software/framework written in C++
    • 5) Feel free to use the remaining items on a daily basis, knowing that C++ is not undermining your life anymore

    Oh... good luck!

  10. Re:python with psutil by dgym · · Score: 3, Insightful

    Having a working Python implementation will also give you a better understanding of which parts are performance/memory sensitive. This may help guide you while rewriting in a different language, or you may find that you can achieve your goals just by hot spot optimizing your Python code using some of these fine tools:

    http://www.numpy.org/ can give you compact arrays of unboxed types and fast operations over them.
    http://cython.org/ is an amazing and versatile tool which allows you to compile your Python code, optionally add type information, optionally manage memory yourself, optionally interact with C/C++ code very easily.
    OpenCL/CUDA if your work can take advantage of them.


    If the standard Python runtime isn't simply too big for your project I can't recommend Cython highly enough. Only tackling the parts that need it is one of the keys to successful optimization and Cython lets you do just that even if it is just one loop in an otherwise pure Python file.

  11. Re:C++ is never the right tool by Anonymous Coward · · Score: 2, Insightful

    So true!

    Personally I don't like to hire people that don't understand low level programming, unless it's for mundane / cheap stuff; no matter what the argument is, the guys that know C / ASM always seem to have a better understanding of problems in general.

  12. Re: C++ is never the right tool by beelsebob · · Score: 4, Insightful

    Lack of garbage collection is one of many reasons why C++ produces fast code. The entire point of using C++ is that you want to have control over how, when and where things are allocated and deallocated.

  13. Re: C++ is never the right tool by beelsebob · · Score: 4, Insightful

    In practice, single desktop class machines with 6000+ concurrent users are not typical use cases. Instead, high performance applications are likely to look more like 3D rendering engines.

    In practice, when you have 16ms to produce a frame, it really matters that the garbage collector doesn't kick in for 2ms once every second, because that'll push you past your frame window and lead to stuttering and dropped frames.

    In practice, it really matters that you can structure your code to make sure no allocations are happening during certain critical operations, because an allocation will potentially need a new page, and the kernel barrier and/or hit locks resulting again, in 1-2ms of unexpected delay, and a dropped frame.

    In practice, it really matters too that you have enough control over memory layout to guarantee that certain structures are all going to end up in cache at the same time, and that you're not going to be doing a bunch of pointer indirection fetching memory during time critical rendering code.

    In practice, modern garbage collection doesn't allow you to solve any of these problems. That is why real time rendering engines are still written in C++, and will continue to be, and why everyone writing them will continue to be glad that C++ is not garbage collected.

  14. Re:If you do go with C++ by narcc · · Score: 4, Insightful

    it would seem to match his requirements nicely.

    We don't even know what his requirements are!