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. Other Discussions
  3. The Weird and The Wonderful
  4. Vintage code

Vintage code

Scheduled Pinned Locked Moved The Weird and The Wonderful
hardware
10 Posts 8 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.
  • B Offline
    B Offline
    BadKarma
    wrote on last edited by
    #1

    A colleague of me retired and his code should be supported. Its old C code from an embedded system that has been ported from assembly.

    BYTE Scan1, Scan2, Scan3, Scan4;
    
    ...
    
    Err = ScanFunction(&Scan1);
    

    That doesn't seems to be so bad, until i saw the Function declaration int ScanFunction(unsigned long* pScan); Later the data from the Scan bytes where placed in a long to work with the correct long value. To be sure it would work even after changes a comment has been placed before the byte declaration /*--------please keep the order of the following bytes as it is. !!!!!!!!!*/ BYTE Scan1, Scan2, Scan3, Scan4

    codito ergo sum

    J C L S T 6 Replies Last reply
    0
    • B BadKarma

      A colleague of me retired and his code should be supported. Its old C code from an embedded system that has been ported from assembly.

      BYTE Scan1, Scan2, Scan3, Scan4;
      
      ...
      
      Err = ScanFunction(&Scan1);
      

      That doesn't seems to be so bad, until i saw the Function declaration int ScanFunction(unsigned long* pScan); Later the data from the Scan bytes where placed in a long to work with the correct long value. To be sure it would work even after changes a comment has been placed before the byte declaration /*--------please keep the order of the following bytes as it is. !!!!!!!!!*/ BYTE Scan1, Scan2, Scan3, Scan4

      codito ergo sum

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      Makes perfect since to me! ;) (Old C programmer) I never liked tricks like that, but they do work in C. I would never recommend doing it though, as there are other ways to accomplish the same task. The only reason, I can think of, for ever doing something like this would be to reduce code, because you were limited by available memory. I have seen cases where adding any new code would have variables stepping on each other. Ok |--->...<---| Opps! |---->...| |...<----|

      INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

      1 Reply Last reply
      0
      • B BadKarma

        A colleague of me retired and his code should be supported. Its old C code from an embedded system that has been ported from assembly.

        BYTE Scan1, Scan2, Scan3, Scan4;
        
        ...
        
        Err = ScanFunction(&Scan1);
        

        That doesn't seems to be so bad, until i saw the Function declaration int ScanFunction(unsigned long* pScan); Later the data from the Scan bytes where placed in a long to work with the correct long value. To be sure it would work even after changes a comment has been placed before the byte declaration /*--------please keep the order of the following bytes as it is. !!!!!!!!!*/ BYTE Scan1, Scan2, Scan3, Scan4

        codito ergo sum

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        hmm. that's strange. i've always seen it done like this:

        BYTE Scan[4];
        ulong *v = (ulong *)&Scan[0];
        ...

        Err = ScanFunction(v);

        much safer.

        image processing toolkits | batch image processing | blogging

        J 1 Reply Last reply
        0
        • B BadKarma

          A colleague of me retired and his code should be supported. Its old C code from an embedded system that has been ported from assembly.

          BYTE Scan1, Scan2, Scan3, Scan4;
          
          ...
          
          Err = ScanFunction(&Scan1);
          

          That doesn't seems to be so bad, until i saw the Function declaration int ScanFunction(unsigned long* pScan); Later the data from the Scan bytes where placed in a long to work with the correct long value. To be sure it would work even after changes a comment has been placed before the byte declaration /*--------please keep the order of the following bytes as it is. !!!!!!!!!*/ BYTE Scan1, Scan2, Scan3, Scan4

          codito ergo sum

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Would be better to encapsulate those 4 bytes in a struct :)

          **

          xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

          **

          1 Reply Last reply
          0
          • B BadKarma

            A colleague of me retired and his code should be supported. Its old C code from an embedded system that has been ported from assembly.

            BYTE Scan1, Scan2, Scan3, Scan4;
            
            ...
            
            Err = ScanFunction(&Scan1);
            

            That doesn't seems to be so bad, until i saw the Function declaration int ScanFunction(unsigned long* pScan); Later the data from the Scan bytes where placed in a long to work with the correct long value. To be sure it would work even after changes a comment has been placed before the byte declaration /*--------please keep the order of the following bytes as it is. !!!!!!!!!*/ BYTE Scan1, Scan2, Scan3, Scan4

            codito ergo sum

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            I'd do it like this:

            union Data
            {
            unsigned long longVal;
            BYTE asBytes[4];
            };
             
            Data d;
            Err = ScanFunction(&d.longVal);

            Steve

            1 Reply Last reply
            0
            • B BadKarma

              A colleague of me retired and his code should be supported. Its old C code from an embedded system that has been ported from assembly.

              BYTE Scan1, Scan2, Scan3, Scan4;
              
              ...
              
              Err = ScanFunction(&Scan1);
              

              That doesn't seems to be so bad, until i saw the Function declaration int ScanFunction(unsigned long* pScan); Later the data from the Scan bytes where placed in a long to work with the correct long value. To be sure it would work even after changes a comment has been placed before the byte declaration /*--------please keep the order of the following bytes as it is. !!!!!!!!!*/ BYTE Scan1, Scan2, Scan3, Scan4

              codito ergo sum

              T Offline
              T Offline
              Tim Carmichael
              wrote on last edited by
              #6

              I had a similiar situation occur, albeit in Fortran; a variable was declared as a byte, but received as a long... Took me a while to find out what was overwriting my data in the calling routine. The overwritten data was fixed values that shouldn't change! Tim

              1 Reply Last reply
              0
              • C Chris Losinger

                hmm. that's strange. i've always seen it done like this:

                BYTE Scan[4];
                ulong *v = (ulong *)&Scan[0];
                ...

                Err = ScanFunction(v);

                much safer.

                image processing toolkits | batch image processing | blogging

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #7

                That may end up in a crash-boom-bang on hardware architectures which do not allow accessing words overlapping word boundaries. You'd have to use a union or #pragma (or something similar) to force alignment on the byte array.

                C 1 Reply Last reply
                0
                • J Jorgen Sigvardsson

                  That may end up in a crash-boom-bang on hardware architectures which do not allow accessing words overlapping word boundaries. You'd have to use a union or #pragma (or something similar) to force alignment on the byte array.

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #8

                  Joergen Sigvardsson wrote:

                  on hardware architectures which do not allow accessing words overlapping word boundaries

                  yeah like i got the time to worry about writing architecture-agnostic code :rolleyes: Windows on Intel is my target.

                  image processing toolkits | batch image processing | blogging

                  J 1 Reply Last reply
                  0
                  • C Chris Losinger

                    Joergen Sigvardsson wrote:

                    on hardware architectures which do not allow accessing words overlapping word boundaries

                    yeah like i got the time to worry about writing architecture-agnostic code :rolleyes: Windows on Intel is my target.

                    image processing toolkits | batch image processing | blogging

                    J Offline
                    J Offline
                    Jorgen Sigvardsson
                    wrote on last edited by
                    #9

                    Yeah, but the other guy might be on some other platform. :) (And no, I couldn't care less about other platforms either, as the stuff I write here will never run on anything but Wintel)

                    1 Reply Last reply
                    0
                    • B BadKarma

                      A colleague of me retired and his code should be supported. Its old C code from an embedded system that has been ported from assembly.

                      BYTE Scan1, Scan2, Scan3, Scan4;
                      
                      ...
                      
                      Err = ScanFunction(&Scan1);
                      

                      That doesn't seems to be so bad, until i saw the Function declaration int ScanFunction(unsigned long* pScan); Later the data from the Scan bytes where placed in a long to work with the correct long value. To be sure it would work even after changes a comment has been placed before the byte declaration /*--------please keep the order of the following bytes as it is. !!!!!!!!!*/ BYTE Scan1, Scan2, Scan3, Scan4

                      codito ergo sum

                      S Offline
                      S Offline
                      ScottM1
                      wrote on last edited by
                      #10

                      I would have done it in binary like so:

                      1101010101001101110010100111010101010011011100101001
                      1010101010011011100101001110101010100110111001010010
                      0101010100110111001010011101010101001101110010100110
                      1101010101001101110010100111010101010011011100100101
                      1101010101001101110010100111010101010011011100101001

                      That way there's less confusion

                      There are 10 types of people in the world, those who understand binary and those who dont.

                      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