Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. New code in C? Where and why, versus C++?

New code in C? Where and why, versus C++?

Scheduled Pinned Locked Moved The Lounge
c++questiondata-structuresooptutorial
58 Posts 24 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Greg UtasG Greg Utas

    As of C++11, there's final[^].

    Robust Services Core | Software Techniques for Lemmings | Articles
    The fox knows many things, but the hedgehog knows one big thing.

    H Offline
    H Offline
    honey the codewitch
    wrote on last edited by
    #15

    How did I forget about "final"? I would have guessed it was newer than C++11 anyway - more like 17 or something. My hero! I guess I've got some code to refactor. Win!

    Real programmers use butterflies

    pkfoxP 1 Reply Last reply
    0
    • H honey the codewitch

      PIEBALDconsult wrote:

      Oh, and of course, I use the "C pre-processor" for various manipulations of my C# code.

      The only reason I don't do that myself is for me intellisense and all that is too big of a productivity win. I respect you that in your case it's not. I can't remember what half the classes and functions are without VS prompting me with them every time I hit ( or . :laugh:

      Real programmers use butterflies

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #16

      No such tools on OpenVMS :D . I never learned the Language Sensitive Editor (LSE).

      1 Reply Last reply
      0
      • H honey the codewitch

        This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?

        Real programmers use butterflies

        Mike HankeyM Offline
        Mike HankeyM Offline
        Mike Hankey
        wrote on last edited by
        #17

        I ran into this problem with the small footprint Arduinos, folks where asking mw why I wanted to use C++ when C was so efficient, faster and used less memory. But I found there really wasn't much difference in size or speed...no response from the peanut gallery. Now with devices with more memory I think that people still use C because that is what they are comfortable with. In the late 80s early 90s I worked for the railroad in the cyber division and I tried to get people to cross over to C++ but not one person could I get to change. I taught classes in using the development tools, debuggers and worked with them one on one...nope!

        The less you need, the more you have. JaxCoder.com

        H M 2 Replies Last reply
        0
        • H honey the codewitch

          This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?

          Real programmers use butterflies

          F Offline
          F Offline
          Forogar
          wrote on last edited by
          #18

          Using a limited set of C++ means almost the same as C but more convenience and a simpler code syntax. You could say you are writing in "C+"!

          - I would love to change the world, but they won’t give me the source code.

          P 1 Reply Last reply
          0
          • H honey the codewitch

            This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?

            Real programmers use butterflies

            D Offline
            D Offline
            Daniel Pfeffer
            wrote on last edited by
            #19

            Part of it is historical reasons, part the perceived overhead of C++ vs C, and part requirements of various Standards (as others have mentioned). For example, neither Linux nor Windows use C in the kernel, because C++ RTL support for the kernel code does not exist. I did see an article about using C++ in Windows kernel drivers. It worked, assuming that (a) you didn't use things like exceptions, virtual function, etc., and (b) you don't use any RTL function that is not defined in the driver library. One place I would probably use C in preference to C++ is hard real-time code. Because of the polymorphism support in C++, it is easier to write code with ill-defined latency in C++ than it is in C, but programmers with sufficient talent can do it in both languages. :)

            Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

            1 Reply Last reply
            0
            • F Forogar

              Using a limited set of C++ means almost the same as C but more convenience and a simpler code syntax. You could say you are writing in "C+"!

              - I would love to change the world, but they won’t give me the source code.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #20

              I like that C/C++ is "multi-paradigm" and I wish C# were too.

              H 1 Reply Last reply
              0
              • S steveb

                Typical C++ baggage is a lots of behind the scenes function calls (ctor, copy ctor,dtor,=, &adressof. So if your program must execute in a certain amount of CPU clocks then C++ is a bad choice. Such as, you do not want to slow down "fly by wire" flap extender module by unnecessary function jumps for example

                R Offline
                R Offline
                Rick York
                wrote on last edited by
                #21

                It is very easy to delete unwanted aspects of classes like copy constructors and copy operators. If you can reduce the amount of dynamic objects used, performance can really improve and deleting those things can help. I have seen this many times.

                "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                S 1 Reply Last reply
                0
                • H honey the codewitch

                  This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?

                  Real programmers use butterflies

                  G Offline
                  G Offline
                  glennPattonWork3
                  wrote on last edited by
                  #22

                  I for one have written C code in C++ compilers from Day One of proper programming just saved them as C files, I have never seen or felt the need to do C++, from the day I wrote a program in Assembler, same in C and again in C++, C & Assembler can't really remember around the same size, C++ three times the size. Okay today better optimisation but still I always get the feeling with C++ theres more going on than I know about. (I do mostly embedded programming)

                  H 1 Reply Last reply
                  0
                  • H honey the codewitch

                    This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?

                    Real programmers use butterflies

                    T Offline
                    T Offline
                    trønderen
                    wrote on last edited by
                    #23

                    If you work in a team with young and eager programmers that completed their degree less than five years ago, there is one good reason: These young geniuses usually love to play around with every single feature of the tools they are given. They like to experiment, and they love to prove that they truly master even the most obscure little detail of the language, the library, the VCS or whatever. So they strive to use all of it, in the most intricate ways that "impresses" even the old coding gurus. C++ is not the language you would want to put in the hands of a young genius for proving that he can do something that no one else can do. You certainly can do dirty tricks in C as well, yet it is far more transparent than the "advanced" C++ features. (Besides, dirty C tricks often require that the programmer is very familiar with hardware and compiler operations, which is not as common with young programmers today, compared to thirty years ago). So C is a better way to discipline young geniuses, by depriving them of tools to create incomprehensive code. (Similar arguments can be used for staying away from git, or Docker orchestration, or CMake, or...)

                    H 1 Reply Last reply
                    0
                    • Mike HankeyM Mike Hankey

                      I ran into this problem with the small footprint Arduinos, folks where asking mw why I wanted to use C++ when C was so efficient, faster and used less memory. But I found there really wasn't much difference in size or speed...no response from the peanut gallery. Now with devices with more memory I think that people still use C because that is what they are comfortable with. In the late 80s early 90s I worked for the railroad in the cyber division and I tried to get people to cross over to C++ but not one person could I get to change. I taught classes in using the development tools, debuggers and worked with them one on one...nope!

                      The less you need, the more you have. JaxCoder.com

                      H Offline
                      H Offline
                      honey the codewitch
                      wrote on last edited by
                      #24

                      Mike Hankey wrote:

                      C was so efficient, faster and used less memory.

                      The last time someone told me that I challenged them to write code in C I couldn't write in C++. I never got a response.

                      Real programmers use butterflies

                      Mike HankeyM 1 Reply Last reply
                      0
                      • G glennPattonWork3

                        I for one have written C code in C++ compilers from Day One of proper programming just saved them as C files, I have never seen or felt the need to do C++, from the day I wrote a program in Assembler, same in C and again in C++, C & Assembler can't really remember around the same size, C++ three times the size. Okay today better optimisation but still I always get the feeling with C++ theres more going on than I know about. (I do mostly embedded programming)

                        H Offline
                        H Offline
                        honey the codewitch
                        wrote on last edited by
                        #25

                        A recurring misconception I see about C++ appears to be the notion that it will write code that you didn't tell it to write, or use memory you didn't tell it to use. It's simply not the case. More the issue is, knowing what you're telling the C++ compiler to generate in terms of code. To wit, there is no code that can be written in C that I cannot write in C++ and generate the equivalent machine instructions - even if doing so means giving up certain niceties like virtual inheritance, exceptions or run time type information. RAII is reason alone to consider minimalistic C++ as an alternative to straight C. It generates no extra code, and it reduces lots of bugs.

                        Real programmers use butterflies

                        G 1 Reply Last reply
                        0
                        • H honey the codewitch

                          This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?

                          Real programmers use butterflies

                          D Offline
                          D Offline
                          David ONeil
                          wrote on last edited by
                          #26

                          honey the codewitch wrote:

                          What is the purpose of writing new code in C for targeting, well ... anything?

                          You should use C because you are a witch, and love casting. :rolleyes:

                          The Science of King David's Court | Object Oriented Programming with C++

                          H 1 Reply Last reply
                          0
                          • T trønderen

                            If you work in a team with young and eager programmers that completed their degree less than five years ago, there is one good reason: These young geniuses usually love to play around with every single feature of the tools they are given. They like to experiment, and they love to prove that they truly master even the most obscure little detail of the language, the library, the VCS or whatever. So they strive to use all of it, in the most intricate ways that "impresses" even the old coding gurus. C++ is not the language you would want to put in the hands of a young genius for proving that he can do something that no one else can do. You certainly can do dirty tricks in C as well, yet it is far more transparent than the "advanced" C++ features. (Besides, dirty C tricks often require that the programmer is very familiar with hardware and compiler operations, which is not as common with young programmers today, compared to thirty years ago). So C is a better way to discipline young geniuses, by depriving them of tools to create incomprehensive code. (Similar arguments can be used for staying away from git, or Docker orchestration, or CMake, or...)

                            H Offline
                            H Offline
                            honey the codewitch
                            wrote on last edited by
                            #27

                            Yeah. Though I might argue that an appropriate LINTing adjusted for dev-house practices upon check-in solves this and numerous other issues. Some developer education is necessary, but unless you can convincingly argue to me that a C developer is cheaper than a C++ developer, I'd just as soon hire the C++ developer, give them the set of practices, and LINT their code. YMMV

                            Real programmers use butterflies

                            1 Reply Last reply
                            0
                            • D David ONeil

                              honey the codewitch wrote:

                              What is the purpose of writing new code in C for targeting, well ... anything?

                              You should use C because you are a witch, and love casting. :rolleyes:

                              The Science of King David's Court | Object Oriented Programming with C++

                              H Offline
                              H Offline
                              honey the codewitch
                              wrote on last edited by
                              #28

                              I use Cisms like casting in C++ because I am a witch and use C++ in places that weren't really meant for full fidelity C++.

                              Real programmers use butterflies

                              D 1 Reply Last reply
                              0
                              • H honey the codewitch

                                I use Cisms like casting in C++ because I am a witch and use C++ in places that weren't really meant for full fidelity C++.

                                Real programmers use butterflies

                                D Offline
                                D Offline
                                David ONeil
                                wrote on last edited by
                                #29

                                I was being facetious, because in C I believe you are often forced to cast where you don't in C++. IE, from one of my articles...

                                SwcTabCtrl* tabControl = (SwcTabCtrl*) tabsC[selectedTabC];

                                in C, whereas C++ is:

                                SwcTabCtrl * tabControl = tabsC[selectedTabC];

                                I don't remember the reason for the forcing of the cast, but I believe it is some C-ism. Anyway, I detest the first code with great detestation. You shouldn't, though, because you are a witch!

                                The Science of King David's Court | Object Oriented Programming with C++

                                W 1 Reply Last reply
                                0
                                • H honey the codewitch

                                  Mike Hankey wrote:

                                  C was so efficient, faster and used less memory.

                                  The last time someone told me that I challenged them to write code in C I couldn't write in C++. I never got a response.

                                  Real programmers use butterflies

                                  Mike HankeyM Offline
                                  Mike HankeyM Offline
                                  Mike Hankey
                                  wrote on last edited by
                                  #30

                                  Exactly, it's like going from B&W to Color. Yeah I'm old enough to remember. :)

                                  The less you need, the more you have. JaxCoder.com

                                  1 Reply Last reply
                                  0
                                  • H honey the codewitch

                                    How did I forget about "final"? I would have guessed it was newer than C++11 anyway - more like 17 or something. My hero! I guess I've got some code to refactor. Win!

                                    Real programmers use butterflies

                                    pkfoxP Offline
                                    pkfoxP Offline
                                    pkfox
                                    wrote on last edited by
                                    #31

                                    I think Java has this ( final not this )

                                    "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

                                    1 Reply Last reply
                                    0
                                    • R Rick York

                                      It is very easy to delete unwanted aspects of classes like copy constructors and copy operators. If you can reduce the amount of dynamic objects used, performance can really improve and deleting those things can help. I have seen this many times.

                                      "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                                      S Offline
                                      S Offline
                                      steveb
                                      wrote on last edited by
                                      #32

                                      They autogenerated by compiler automatically even if the code does not declare them

                                      R 1 Reply Last reply
                                      0
                                      • S steveb

                                        They autogenerated by compiler automatically even if the code does not declare them

                                        R Offline
                                        R Offline
                                        Rick York
                                        wrote on last edited by
                                        #33

                                        Yes, I know and as I wrote they can be deleted or, in other words, effectively removed from usage. I do this to make a class uncopyable :

                                        #define MakeUncopyable( className ) \
                                        className( const className & ) = delete; \
                                        className & operator=( const className & ) = delete;

                                        and any attempts to copy the object will fail to compile. This is useful for singleton objects and others where there can be only one.

                                        "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                                        1 Reply Last reply
                                        0
                                        • H honey the codewitch

                                          This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?

                                          Real programmers use butterflies

                                          G Offline
                                          G Offline
                                          giulicard
                                          wrote on last edited by
                                          #34

                                          Yes. I agree. To not mention that with the "placement new" operator is possible to create objects on existing memory blocks. Turning off the exception mechanism C++ is very good for the development of real-time embedded applications. Recreating the polymorfism in C, at best, has the same speed as in C++. But most of the time, C++ beats C in many areas, as the latter does not have templates. It's only possible to emulate them with macros, but macros, most of the time, are ugly and dangerous. Templates help with performances: for example, qsort has to do a callback to compare items; so, it spends CPU cycles, fools the CPU branch predictor, and possibly invalidates the level-1 instruction cache. With std::sort (or std::stable_sort) it's possible "to inject" the comparison into the algorithm. Yes, templates could make code bloating, but judicious use of non-template base classes could mitigate it.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups