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. I love C where types are basically a suggestion rather than a rule

I love C where types are basically a suggestion rather than a rule

Scheduled Pinned Locked Moved The Lounge
data-structures
77 Posts 35 Posters 2 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.
  • R Rick York

    Then there are unions. Essentially a compiler-driven cast with no type conversions what so ever. It's in-memory aliasing. It takes casting to an entirely new level. I have found many good uses for unions. So has Microsoft - see the LARGE_INTEGER for an example.

    "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?"

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

    Yeah, I'm aware - I even adapted the approach for C# using StructLayout.Explicit. It's just that declaring a union type isn't always called for. If you're doing the cast once or twice it doesn't make a lot of sense, so i do it when it's called for.

    Real programmers use butterflies

    1 Reply Last reply
    0
    • T TheGreatAndPowerfulOz

      yeah, that'll work so long as you don't stash your structs pointers this way. :)

      #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

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

      Yeah I try not to point into the stack for longer than the context of the containing function. :laugh:

      Real programmers use butterflies

      1 Reply Last reply
      0
      • D den2k88

        Yes to all of that. Elegant and concise. C++ is much more type safe, and yet it became heavy: hard to read and often very hard to write, a bit constrictive. C is absolute freedom, total power... and requires total responsibility.

        GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

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

        den2k88 wrote:

        C++ is ... hard to read and often very hard to write

        Am I using it wrong, because I've never had these problems?

        “If we get $100,000, we will go to Potato blockchain.” Enable the dream!

        T Greg UtasG D R 4 Replies Last reply
        0
        • D David ONeil

          den2k88 wrote:

          C++ is ... hard to read and often very hard to write

          Am I using it wrong, because I've never had these problems?

          “If we get $100,000, we will go to Potato blockchain.” Enable the dream!

          T Offline
          T Offline
          TheGreatAndPowerfulOz
          wrote on last edited by
          #31

          Funny, I was thinking the same things. But then again, I've encountered code like the OP described. Usually written by electrical engineers.

          #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

          Greg UtasG 1 Reply Last reply
          0
          • T TheGreatAndPowerfulOz

            Funny, I was thinking the same things. But then again, I've encountered code like the OP described. Usually written by electrical engineers.

            #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

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

            :laugh: You mean C++ that is no more than C or, even worse, FORTRAN?

            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>

            T 1 Reply Last reply
            0
            • D David ONeil

              den2k88 wrote:

              C++ is ... hard to read and often very hard to write

              Am I using it wrong, because I've never had these problems?

              “If we get $100,000, we will go to Potato blockchain.” Enable the dream!

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

              Well, some of the template stuff out there... :wtf:

              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>

              D 1 Reply Last reply
              0
              • T TheGreatAndPowerfulOz

                yeah, that'll work so long as you don't stash your structs pointers this way. :)

                #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

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

                No problem. Placement new to the rescue! Sorry, wrong language. :laugh:

                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>

                T 1 Reply Last reply
                0
                • H honey the codewitch

                  The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

                  Real programmers use butterflies

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #35

                  the file is also not compatible between architecture to the next! :( i.e. endianess for sure.. but probably computer struct paddings too! :/ though haven't used C in decades, so I am not so sure about that one...

                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                  H 1 Reply Last reply
                  0
                  • S Super Lloyd

                    the file is also not compatible between architecture to the next! :( i.e. endianess for sure.. but probably computer struct paddings too! :/ though haven't used C in decades, so I am not so sure about that one...

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

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

                    Sometimes that doesn't matter. Sometimes you're writing software dedicated to a particular microcontroller and it expects the thing to be wired a certain way in order to run anyway.

                    Real programmers use butterflies

                    S 1 Reply Last reply
                    0
                    • H honey the codewitch

                      Sometimes that doesn't matter. Sometimes you're writing software dedicated to a particular microcontroller and it expects the thing to be wired a certain way in order to run anyway.

                      Real programmers use butterflies

                      S Offline
                      S Offline
                      Super Lloyd
                      wrote on last edited by
                      #37

                      oh well.. yes if consumer and producer is the same one, I guess it's all good! :)

                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                      1 Reply Last reply
                      0
                      • Greg UtasG Greg Utas

                        Well, some of the template stuff out there... :wtf:

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

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

                        When that becomes easy to read, you know you are a real programmer!

                        “If we get $100,000, we will go to Potato blockchain.” Enable the dream!

                        1 Reply Last reply
                        0
                        • Greg UtasG Greg Utas

                          :laugh: You mean C++ that is no more than C or, even worse, FORTRAN?

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

                          T Offline
                          T Offline
                          TheGreatAndPowerfulOz
                          wrote on last edited by
                          #39

                          Yeah, that too. But yes, c++ that was like trying to interpret sanskrit.

                          #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                          1 Reply Last reply
                          0
                          • Greg UtasG Greg Utas

                            No problem. Placement new to the rescue! Sorry, wrong language. :laugh:

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

                            T Offline
                            T Offline
                            TheGreatAndPowerfulOz
                            wrote on last edited by
                            #40

                            Yeah, but that processing the struct after-the-fact. Her technique is great for 'simple' structs.

                            #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                            1 Reply Last reply
                            0
                            • H honey the codewitch

                              The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

                              Real programmers use butterflies

                              J Offline
                              J Offline
                              Jorgen Andersson
                              wrote on last edited by
                              #41

                              I've read this subject line several times now, wanting to point out what's so wrong with it. Anyway, I think you will love Javascript.

                              Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                              H 1 Reply Last reply
                              0
                              • J Jorgen Andersson

                                I've read this subject line several times now, wanting to point out what's so wrong with it. Anyway, I think you will love Javascript.

                                Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

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

                                There's a special place in programmer hell for whoever came up with duck typing.

                                Real programmers use butterflies

                                1 Reply Last reply
                                0
                                • D Daniel Pfeffer

                                  [Program: Shoot in foot](http://www.personal.psu.edu/sxt104/program1.html)

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

                                  F Offline
                                  F Offline
                                  fd9750
                                  wrote on last edited by
                                  #43

                                  The one about Forth is right on the mark. :-D

                                  1 Reply Last reply
                                  0
                                  • H honey the codewitch

                                    The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

                                    Real programmers use butterflies

                                    F Offline
                                    F Offline
                                    fd9750
                                    wrote on last edited by
                                    #44

                                    I have been using C for+/- 32 years and still like it a lot. The fact that yes, you can do anything, does force you to be very careful what you ask for because you will get it. :-\

                                    H 1 Reply Last reply
                                    0
                                    • D David ONeil

                                      den2k88 wrote:

                                      C++ is ... hard to read and often very hard to write

                                      Am I using it wrong, because I've never had these problems?

                                      “If we get $100,000, we will go to Potato blockchain.” Enable the dream!

                                      D Offline
                                      D Offline
                                      den2k88
                                      wrote on last edited by
                                      #45

                                      Neither did I, until I discovered I "was using it wrong" and had some "true C++ programmer" abusing templates lambda functions like there was no tomorrow.

                                      GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                      D 1 Reply Last reply
                                      0
                                      • D Daniel Pfeffer

                                        [Program: Shoot in foot](http://www.personal.psu.edu/sxt104/program1.html)

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

                                        R Offline
                                        R Offline
                                        Rage
                                        wrote on last edited by
                                        #46

                                        The C++ one is even more accurate when pointers are involved !

                                        Do not escape reality : improve reality !

                                        1 Reply Last reply
                                        0
                                        • H honey the codewitch

                                          The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

                                          Real programmers use butterflies

                                          C Offline
                                          C Offline
                                          ColinBurnell
                                          wrote on last edited by
                                          #47

                                          You could always use a union, no need to even cast then (: I wouldn't want to program in C targeting a PC these days; but it is good for 8 & 16 bit embedded work, where you need to get at hardware registers and only have maybe 2K RAM and 16K or 32K ROM.

                                          H 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