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.
  • H honey the codewitch

    Yeah the vtable thing might be overkill, but I don't like extra hidden overhead on my classes. I'd rather make inheritance not a thing in many cases** ** you can do inheritance, you just have to be careful about who holds things that need to be freed when you don't have a virtual destructor. Most of the time in these cases, I factor my code such that my data is polymorphic, and my classes wrap that. It avoids the issue.

    Real programmers use butterflies

    Greg UtasG Offline
    Greg UtasG Offline
    Greg Utas
    wrote on last edited by
    #11

    If it's a one-witch show, great. If the whole coven is beavering away on it, you'll be hard-pressed to dispel Pareto. :)

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

    <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
    <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

    H 1 Reply Last reply
    0
    • Greg UtasG Greg Utas

      If it's a one-witch show, great. If the whole coven is beavering away on it, you'll be hard-pressed to dispel Pareto. :)

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

      I think it depends on the size and the complexity of the project, ultimately, and given I don't have much room to work with on these little systems, I think it's okay. Maybe not ideal but okay. I've simply taken to being very careful not to let you inherit classes you shouldn't inherit from. I wish C++ had a "sealed" keyword though.

      Real programmers use butterflies

      Greg UtasG 1 Reply Last reply
      0
      • P PIEBALDconsult

        I've only ever dabbled in C++ and I had little use for it. From the early 90s until 2002 I used ANSI C (on OpenVMS). On my PC I have a few C/C++ compilers, but on three of my OpenVMS systems I've installed C (not C++) to play with on occasion. DEC C V6.0-001 on OpenVMS Alpha V7.2 Compaq C V6.4-005 on OpenVMS VAX V7.3 HP C V7.3-009 on OpenVMS Alpha V8.3 (I don't have C on my Itanium server. I may need to address that) The only thing I currently use C (not C++) for on my PC is an experiment in using ODBC (using a book from 1999). Oh, and of course, I use the "C pre-processor" for various manipulations of my C# code.

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

        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 1 Reply Last reply
        0
        • H honey the codewitch

          I think it depends on the size and the complexity of the project, ultimately, and given I don't have much room to work with on these little systems, I think it's okay. Maybe not ideal but okay. I've simply taken to being very careful not to let you inherit classes you shouldn't inherit from. I wish C++ had a "sealed" keyword though.

          Real programmers use butterflies

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #14

          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.

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          H 1 Reply Last reply
          0
          • 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
                  • 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
                    #19

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

                    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

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

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