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. Buy five, Pay for eight

Buy five, Pay for eight

Scheduled Pinned Locked Moved The Lounge
questionjavascriptdata-structureshelp
8 Posts 5 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.
  • G Offline
    G Offline
    Georg Haan
    wrote on last edited by
    #1

    I don't use fora much, but once in a while I come across a problem which I simply can't explain. I'm writing this little scripting language interpreter, which for some reason - probably because of me overestimating my spare time - will be able to interpret JavaScript and VBScript. So far it can lexalize, meaning it can convert plaintext code ("if (myVar==2)DoThis();") to Codes, which I, regardless of it actually signifying an operation, call OpCodes. These OpCodes are listed in a huge array of the type CODE which I'll define here: struct CODE { unsigned char cType; union { DWORD iValue; FARPROC lpAdress; unsigned char * lpString; }; }; I will spare you all the big idea behind this, and ask my question: Why does sizeof(CODE) equal 8, and not 5? Georg Haan (NL)

    D T realJSOPR G 4 Replies Last reply
    0
    • G Georg Haan

      I don't use fora much, but once in a while I come across a problem which I simply can't explain. I'm writing this little scripting language interpreter, which for some reason - probably because of me overestimating my spare time - will be able to interpret JavaScript and VBScript. So far it can lexalize, meaning it can convert plaintext code ("if (myVar==2)DoThis();") to Codes, which I, regardless of it actually signifying an operation, call OpCodes. These OpCodes are listed in a huge array of the type CODE which I'll define here: struct CODE { unsigned char cType; union { DWORD iValue; FARPROC lpAdress; unsigned char * lpString; }; }; I will spare you all the big idea behind this, and ask my question: Why does sizeof(CODE) equal 8, and not 5? Georg Haan (NL)

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      Two main reasons: 1. Wrong forum 2. Data alignment (see #pragma pack) Crivo Automated Credit Assessment

      G 1 Reply Last reply
      0
      • G Georg Haan

        I don't use fora much, but once in a while I come across a problem which I simply can't explain. I'm writing this little scripting language interpreter, which for some reason - probably because of me overestimating my spare time - will be able to interpret JavaScript and VBScript. So far it can lexalize, meaning it can convert plaintext code ("if (myVar==2)DoThis();") to Codes, which I, regardless of it actually signifying an operation, call OpCodes. These OpCodes are listed in a huge array of the type CODE which I'll define here: struct CODE { unsigned char cType; union { DWORD iValue; FARPROC lpAdress; unsigned char * lpString; }; }; I will spare you all the big idea behind this, and ask my question: Why does sizeof(CODE) equal 8, and not 5? Georg Haan (NL)

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        Alignment of structures. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

        1 Reply Last reply
        0
        • G Georg Haan

          I don't use fora much, but once in a while I come across a problem which I simply can't explain. I'm writing this little scripting language interpreter, which for some reason - probably because of me overestimating my spare time - will be able to interpret JavaScript and VBScript. So far it can lexalize, meaning it can convert plaintext code ("if (myVar==2)DoThis();") to Codes, which I, regardless of it actually signifying an operation, call OpCodes. These OpCodes are listed in a huge array of the type CODE which I'll define here: struct CODE { unsigned char cType; union { DWORD iValue; FARPROC lpAdress; unsigned char * lpString; }; }; I will spare you all the big idea behind this, and ask my question: Why does sizeof(CODE) equal 8, and not 5? Georg Haan (NL)

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          Because a union will always have a size equal to the largest possible combination of union types. So, regardless of which cType you're using, your CODE structure is always the same size to accomodate the largest possible combination of fields. Isn't there a better way to do this than using a union? I'd probably use a class that contains a variant before using a union. Variants are fairly well-documented and can hold any type you might be able to dream up. "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          L 1 Reply Last reply
          0
          • D Daniel Turini

            Two main reasons: 1. Wrong forum 2. Data alignment (see #pragma pack) Crivo Automated Credit Assessment

            G Offline
            G Offline
            Georg Haan
            wrote on last edited by
            #5
            1 Reply Last reply
            0
            • realJSOPR realJSOP

              Because a union will always have a size equal to the largest possible combination of union types. So, regardless of which cType you're using, your CODE structure is always the same size to accomodate the largest possible combination of fields. Isn't there a better way to do this than using a union? I'd probably use a class that contains a variant before using a union. Variants are fairly well-documented and can hold any type you might be able to dream up. "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              L Offline
              L Offline
              Luis Alonso Ramos
              wrote on last edited by
              #6

              Actually he's right on that one (expecting it to be 5)... the char is one byte long, and any of the members of the union (DWORD, FARPROC, char *) are four bytes long, so added up, everuthing should be 5 bytes long. But thanks to alignment, it' is 8 (I guess, that's what someone said above) -- LuisR --------   Luis Alonso Ramos   Chihuahua, Mexico   www.luisalonsoramos.com

              1 Reply Last reply
              0
              • G Georg Haan

                I don't use fora much, but once in a while I come across a problem which I simply can't explain. I'm writing this little scripting language interpreter, which for some reason - probably because of me overestimating my spare time - will be able to interpret JavaScript and VBScript. So far it can lexalize, meaning it can convert plaintext code ("if (myVar==2)DoThis();") to Codes, which I, regardless of it actually signifying an operation, call OpCodes. These OpCodes are listed in a huge array of the type CODE which I'll define here: struct CODE { unsigned char cType; union { DWORD iValue; FARPROC lpAdress; unsigned char * lpString; }; }; I will spare you all the big idea behind this, and ask my question: Why does sizeof(CODE) equal 8, and not 5? Georg Haan (NL)

                G Offline
                G Offline
                Georg Haan
                wrote on last edited by
                #7

                If I pragma pack that union, like this: #pragma pack(1) // aligment of 1 (was 4) ... union declaration ... #pragma pack(4) // aligment of 4 this would save the 3 bytes that I'd never use (right?). But does handling it become slower due to this? Georg Haan

                D 1 Reply Last reply
                0
                • G Georg Haan

                  If I pragma pack that union, like this: #pragma pack(1) // aligment of 1 (was 4) ... union declaration ... #pragma pack(4) // aligment of 4 this would save the 3 bytes that I'd never use (right?). But does handling it become slower due to this? Georg Haan

                  D Offline
                  D Offline
                  Daniel Turini
                  wrote on last edited by
                  #8

                  Georg Haan wrote: this would save the 3 bytes that I'd never use (right?). Yes Georg Haan wrote: But does handling it become slower due to this? Yes But nobody will notice that you'll be saving space or time, unless your app alocates 1000000 objects. Crivo Automated Credit Assessment

                  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