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. Someone is reading my mind!

Someone is reading my mind!

Scheduled Pinned Locked Moved The Lounge
comquestiondiscussioncareer
27 Posts 9 Posters 0 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.
  • J Joe Woodbury

    I swear this guy is reading my mind, only doing a better job expressing my thoughts. (I'm quite sure many CPians will have a cow about this guy's rants; we'll lose a few more when their head explode:).) http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1[^]

    Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

    P Offline
    P Offline
    peterchen
    wrote on last edited by
    #18

    this guy wrote:

    Many people still don’t understand what was amiss with C-style cast. Neither do I

    Well, I do. After spending 4 days on hunting someone elses C-Style cast mistake through a dozen of class templates, I really do.


    Some of us walk the memory lane, others plummet into a rabbit hole
    Tree in C# || Fold With Us! || sighist

    Steve EcholsS 1 Reply Last reply
    0
    • P peterchen

      this guy wrote:

      Many people still don’t understand what was amiss with C-style cast. Neither do I

      Well, I do. After spending 4 days on hunting someone elses C-Style cast mistake through a dozen of class templates, I really do.


      Some of us walk the memory lane, others plummet into a rabbit hole
      Tree in C# || Fold With Us! || sighist

      Steve EcholsS Offline
      Steve EcholsS Offline
      Steve Echols
      wrote on last edited by
      #19

      Would a static/reinterpret/dynamic_cast have helped you?


      - S 50 cups of coffee and you know it's on!

      • S
        50 cups of coffee and you know it's on!
        Code, follow, or get out of the way.
      P 1 Reply Last reply
      0
      • Steve EcholsS Steve Echols

        Would a static/reinterpret/dynamic_cast have helped you?


        - S 50 cups of coffee and you know it's on!

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

        to be fair: no it was a reinterpret_cast to the wrong class. But still: knowing about why there are different cast operators might have helped the original developer thinking twice. (FWIW The different cast operators are no more than what Raymond Chen would call "developer tax on C++". I hate the syntax, too, but it looks like it was deliberately chosen to allow creating your own cast operators: template <typename Target> Target * peterchens_cast(void * p) { return (Target *) ((DWORD *)p + *(DWORD *)p); } X * x = peterchens_cast<X>(0); // boom! note the deliberate use of a C style cast to avoid manually escaping the arrowheads.


        Some of us walk the memory lane, others plummet into a rabbit hole
        Tree in C# || Fold With Us! || sighist

        M 1 Reply Last reply
        0
        • Steve EcholsS Steve Echols

          I totally agree. Long live C-style casts! The new casts are just pompous. I don't see how the new casts protect you in any way. You still have to know why you're casting and the ramifications of it. Maybe this is why they came up with c#? :-D


          - S 50 cups of coffee and you know it's on!

          A Offline
          A Offline
          Anna Jayne Metcalfe
          wrote on last edited by
          #21

          If C style casts were safe, they'd be OK. Unfortunately they simply aren't...by using them you are effectively saying to the compiler "I know exactly what I'm doing" when in most cases you haven't even thought about it. A case in point: in one client's code I've studied I found a pointer to an object (lets call it 'A'), derived from 'B', but accessed via a type 'C' (also derived from 'B') pointer! The only reason it didn't crash was that the only methods accessed belonged to the base class. X| As a result of issues like this, C style casts are banned in all new code here. We use PC-Lint to enforce that restriction rigidly. :rose:

          Anna :rose: Currently working mostly on: Visual Lint :cool: Anna's Place | Tears and Laughter "Be yourself - not what others think you should be" - Marcia Graesch "Anna's just a sexy-looking lesbian tart" - A friend, trying to wind me up. It didn't work.

          L 1 Reply Last reply
          0
          • Steve EcholsS Steve Echols

            Okay, I hear all these arguments for the new-style casts, but they just provide a false sense of security, right? You can still screw yourself (or others) by using the wrong new style cast operator. Aren't the new cast operators provided to imply intent, and enforce it, based on intent? If you get the intent wrong, well then you're know better off than old school C-style casts, right?


            - S 50 cups of coffee and you know it's on!

            A Offline
            A Offline
            Anna Jayne Metcalfe
            wrote on last edited by
            #22

            Not really. For example, the compiler won't let you use static_cast where reinterpret_cast is required, or vice versa. The only ones that can be interchangeable under certain circumstances are static_cast and dynamic_cast. As a result, the (usually very few) reinterpret_casts stand out in a piece of code, and are likely to be more throughly scrutinised than would be the case if they were C-style casts. :rose:

            Anna :rose: Currently working mostly on: Visual Lint :cool: Anna's Place | Tears and Laughter "Be yourself - not what others think you should be" - Marcia Graesch "Anna's just a sexy-looking lesbian tart" - A friend, trying to wind me up. It didn't work.

            1 Reply Last reply
            0
            • J Joe Woodbury

              Shog9 wrote:

              I've encountered more bugs related to C-style casts than i care to remember.

              Casting bugs are probably the least common bugs I've seen. I'm struggling right now to remember the last one I dealt with. The only near candidate from three years ago in ten year old 'C' code doesn't count because the entire function was screwed up due to a stack overrun in a failed condition that had nothing to do with the original developer casting away a const [which was a error and unrelated to the overwrite condition].)

              Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

              A Offline
              A Offline
              Anna Jayne Metcalfe
              wrote on last edited by
              #23

              I don't find them that often, but usually when I do they are really nasty little critters. Half the problem is that you have to hunt through all the reasonable examples to find the screwballs. It's far, far easier to just search for the unsafe casts (i.e. reinterpret_cast) to check them specifically. FYI I find variable scoping issues more common (e.g. hiding of an enclosing scope) in client code. Very few compilers warn about such occurrences, but they can cause huge changes in behaviour under the wrong circumstances. We check for both in our new code, along with a whole horde of other issues. Our lint warning policy is very strict, and so far it has paid off. :)

              Anna :rose: Currently working mostly on: Visual Lint :cool: Anna's Place | Tears and Laughter "Be yourself - not what others think you should be" - Marcia Graesch "Anna's just a sexy-looking lesbian tart" - A friend, trying to wind me up. It didn't work.

              1 Reply Last reply
              0
              • A Anna Jayne Metcalfe

                If C style casts were safe, they'd be OK. Unfortunately they simply aren't...by using them you are effectively saying to the compiler "I know exactly what I'm doing" when in most cases you haven't even thought about it. A case in point: in one client's code I've studied I found a pointer to an object (lets call it 'A'), derived from 'B', but accessed via a type 'C' (also derived from 'B') pointer! The only reason it didn't crash was that the only methods accessed belonged to the base class. X| As a result of issues like this, C style casts are banned in all new code here. We use PC-Lint to enforce that restriction rigidly. :rose:

                Anna :rose: Currently working mostly on: Visual Lint :cool: Anna's Place | Tears and Laughter "Be yourself - not what others think you should be" - Marcia Graesch "Anna's just a sexy-looking lesbian tart" - A friend, trying to wind me up. It didn't work.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #24

                Anna-Jayne Metcalfe wrote:

                I know exactly what I'm doing"

                I do. Thats why I call myself an engineer and charge vast ammounts of money.

                Anna-Jayne Metcalfe wrote:

                C style casts are banned

                Yet more dogma. If anyone cant handle this: serverID = LongEndianSwap(*((PULONG)(&pDHCPpacket->dhcp.options[i+2]))); Then dont write programs.

                Truth is the subjection of reality to an individuals perception

                A 1 Reply Last reply
                0
                • L Lost User

                  Anna-Jayne Metcalfe wrote:

                  I know exactly what I'm doing"

                  I do. Thats why I call myself an engineer and charge vast ammounts of money.

                  Anna-Jayne Metcalfe wrote:

                  C style casts are banned

                  Yet more dogma. If anyone cant handle this: serverID = LongEndianSwap(*((PULONG)(&pDHCPpacket->dhcp.options[i+2]))); Then dont write programs.

                  Truth is the subjection of reality to an individuals perception

                  A Offline
                  A Offline
                  Anna Jayne Metcalfe
                  wrote on last edited by
                  #25

                  fat_boy wrote:

                  I do. Thats why I call myself an engineer and charge vast ammounts of money.

                  So do I. That doesn't mean everybody else does though...and somebody has to sort out the mess clients are left in by less experienced developers...

                  fat_boy wrote:

                  Then dont write programs.

                  Ah but they still do, and they often muck it up...

                  Anna :rose: Currently working mostly on: Visual Lint :cool: Anna's Place | Tears and Laughter "Be yourself - not what others think you should be" - Marcia Graesch "Anna's just a sexy-looking lesbian tart" - A friend, trying to wind me up. It didn't work.

                  1 Reply Last reply
                  0
                  • P peterchen

                    to be fair: no it was a reinterpret_cast to the wrong class. But still: knowing about why there are different cast operators might have helped the original developer thinking twice. (FWIW The different cast operators are no more than what Raymond Chen would call "developer tax on C++". I hate the syntax, too, but it looks like it was deliberately chosen to allow creating your own cast operators: template <typename Target> Target * peterchens_cast(void * p) { return (Target *) ((DWORD *)p + *(DWORD *)p); } X * x = peterchens_cast<X>(0); // boom! note the deliberate use of a C style cast to avoid manually escaping the arrowheads.


                    Some of us walk the memory lane, others plummet into a rabbit hole
                    Tree in C# || Fold With Us! || sighist

                    M Offline
                    M Offline
                    Miszou
                    wrote on last edited by
                    #26

                    Another reason that I've heard for the cast syntax, is that is makes searching for casts much easier - something that may have helped you locate your problem a little sooner. It's much easier to search for the word "_cast" to find all casts in a file than it is to search for an opening paranthesis or a regular expression. Maybe this isn't the best reason for using the new cast operators, but it's one more point in favor of them.


                    The StartPage Randomizer | The Timelapse Project | A Random Web Page

                    P 1 Reply Last reply
                    0
                    • M Miszou

                      Another reason that I've heard for the cast syntax, is that is makes searching for casts much easier - something that may have helped you locate your problem a little sooner. It's much easier to search for the word "_cast" to find all casts in a file than it is to search for an opening paranthesis or a regular expression. Maybe this isn't the best reason for using the new cast operators, but it's one more point in favor of them.


                      The StartPage Randomizer | The Timelapse Project | A Random Web Page

                      P Offline
                      P Offline
                      peterchen
                      wrote on last edited by
                      #27

                      Interesting point.


                      Some of us walk the memory lane, others plummet into a rabbit hole
                      Tree in C# || Fold With Us! || sighist

                      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