![]() |
|
#1
|
|||
|
|||
strange sizeof(structure) - multiple of 8Hi all, I'm a bit confused about this:
I'm using a structure containing long ints, ints, doubles, chars and array of chars; however I try to change the number of fileds in this structure (ie. adding one or more ints or chars) when I call the sizeof() of this structure it returns a number larger than the sum of all sizeof() for each field - number which, after hours of struggle, I found to be a multiple of 8 nomatter what!! So no matter how many fields my structure has, as long as it contains both numbers and chars, the sizeof() the whole thing returns a multiple of 8 . My question is: why ? |
|
#2
|
|||
|
|||
further detailsI know I did not make myself very clear, here's an example:
CPP / C++ / C Code:
Output to console is: ----------------------- Size Of my char : 1 Size Of my int : 4 Size Of my structure : 8 ----------------------- This thing returns 8 when it should ( sizeof(a_char) + sizeof(a_num) ) that is 5. I'm using a Digital UNIX V4.0E (Rev. 1091). thanks. |
|
#3
|
|||
|
|||
|
My guess is that the entire structure will take up memory in multiples of the size of its biggest member. if your structure contains 7 chars, it will take up 7 bytes in total. if your structure contains 1 int and 3 chars, it will take up 8 bytes. if your structure contains 1 int and 5 chars, it will take up 12 bytes. Same goes for char*. If it contains 1 double and 1 int, it will take up 16 bytes.
This doesnt apply to structures within structures. for example if you had a struct which is 5 bytes within a struct which has 2 chars, it will take up 7 bytes instead of 10 bytes. Only applies to c primitive data types. I think it has to do with the way it's stored in memory and/or registers. I will find out why it happens exactly and post it later on. __________________
spasms!!! |
|
#4
|
|||
|
|||
|
Thanks, machinated.
However I can tell you that even with 1 int and 5 chars it will not allocate 12 but 16; however the reason must be the one you mentioned. Thanks again. |
|
#5
|
|||
|
|||
|
Quote:
that's weird because when i try it it allocates 12 bytes. __________________
spasms!!! |
|
#6
|
|||
|
|||
|
Well it is weird then, cause I've tried it more than once, and it shows sizeof() as 16.. no doubt it must be a compiler thing, but don't bother about it, it's not that important.
Thanks again for your support ! |
|
#7
|
||||
|
||||
|
Quote:
So, if you define a structure with 17 bytes, the system will allocate 24 (if allignment is on 8-byte boundaries and not 4) __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#8
|
|||
|
|||
|
Quote:
Aha.. good to know; I never read this anywhere, so - thanks alot for clearing it out . However if I try a structure of 17 chars, either 17 fields of char or a char array[17], the sizeof() returns the right, precise, 17 bytes. The padding phenomenon only happens when alternating a number type with a char type. Greetings. |
|
#9
|
|||
|
|||
|
yes exactly. if you have 5 chars, ur structure will take up 5 bytes. So it does have to do with the content of your structure.
Each type has an alignment or padding assigned to it in the compiler. char has 1 byte, short has 2, integer has 4, and double has 8 in MSVC. In GCC, double has 4 bytes of alignment (which i think is very smart since a register is 4 bytes). Apparently in your compiler, integer has 8 bytes of alignment which is a waste __________________
spasms!!! |
|
#10
|
||||
|
||||
|
Quote:
Not exactly. Take this program: CPP / C++ / C Code:
When I compile it normally, the output I get is 8 8 12 12 12 12 16 When I compile it with the 'packed' switch, it's 7 8 9 10 11 12 13 The packed switch tell the compiler not to pad data to align values on word boundaries. Generally, the compiler will pad data unless told otherwise. This is what's happening to you. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Cd burner behaves strange | Julepessimisten | Computer Hardware Forum | 0 | 03-Jan-2004 15:17 |
| Customising SMS sending script via e-mail gateway for multiple users | jrobbio | MySQL / PHP Forum | 4 | 29-Sep-2003 13:49 |
| multiple Cpanel/WHM servers manager | simscripts | Web Hosting Forum | 0 | 09-Sep-2003 19:10 |
| I am getting STRANGE emails / feedback today! | JdS | Learning Journal by J de Silva | 4 | 24-Aug-2003 19:16 |
| SQL multiple languages query | samtediou | MySQL / PHP Forum | 6 | 23-Jul-2003 14:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The