Buy five, Pay for eight
-
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)
-
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)
Two main reasons: 1. Wrong forum 2. Data alignment (see #pragma pack) Crivo Automated Credit Assessment
-
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)
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?
-
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)
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
-
Two main reasons: 1. Wrong forum 2. Data alignment (see #pragma pack) Crivo Automated Credit Assessment
-
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
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
-
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)
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
-
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
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