🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How 2 know whether comp stores 16<=bitsbigflags 2 same 32bitsbig blocks

Started by
4 comments, last by Afterlife 22 years, 9 months ago
Topic in english (tried to compress it so it would fit, but in vain): How do I know whether if my computer stores 16 or less bits big variables into same 32 bit big blocks or not? The size of a struct with with three 'usigned : 1's is 4 bytes. Sizeof doesn't recognize unsigned : ? definitions correctly, it reports them as integers, but only exceeds 4 bytes if there is over 32 'unsigned : 1's defined. In that case sizeof would report the size of the struct as 8 bytes. But the fact that a size of a struct, that has more than one variable, is less than 32 bytes doesn't necessarily mean that all of the variables were saved in the same 32 bit block, or does it? Edited by - Afterlife on September 21, 2001 8:25:36 AM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Advertisement
sizeof reports the amount of memory (in bytes) a certain type occupies. In your case with only a few bits used, the struct's size is still rounded up to the nearest multiple of a basic machine word (32 bits in your case). This is done for alignment reasons etc.

Edited by - Dactylos on September 21, 2001 8:28:11 AM
But the main question was, How do I know if the program(by its contents), with one struct that has three above explained variables, takes up 3*32 bits (if the small 1 bit sized variables each have their own 32 bit memory block) or 1*32 bits (if all the small variables are put into the same 32 bit memory block)?

sturct BLAH {unsigned one : 1, two : 1, three : 2};

BLAH -> memory [0=one, 1=two, 2=three, 3=three, 4=empty ... 31=empty]
=>1*32 bits

or

BLAH -> memory [0=one, 1=empty, 2=empty, 3=empty, 4=empty ... 31=empty]
BLAH -> memory [0=two, 1=empty, 2=empty, 3=empty, 4=empty ... 31=empty]
BLAH -> memory [0=three, 1=three, 2=empty, 3=empty, 4=empty ... 31=empty]
=> 3*32bits

?

I was told it varies on different computers/systems. How do I know which method my computer uses? According to the sizeof function the struct BLAH only takes less than 32 bits, but is the sizeof function trustable in this case?

Edited by - Afterlife on September 21, 2001 10:52:15 AM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
You can always trust the sizeof operator to return the number of bytes occupied by a certain type (provided that it''s a complete type, so the compiler can actually calculate its size).

I don''t really understand your problem, even though I do understand your question.
quote: But the main question was, How do I know if the program(by its contents), with one struct that has three above explained variables, takes up 3*32 bits (if the small 1 bit sized variables each have their own 32 bit memory block) or 1*32 bits (if all the small variables are put into the same 32 bit memory block)?

If the three one-bit variables each get their own 32 bit word, then sizeof will return 3*32 = 96. If on the other hand all three variables are stored within one 32 bit word, then sizeof will report 1*32 = 32.

Does this make things clearer?
Yeah, if that''s a fact. Thanks. I just wasn''t sure if sizeof just counts the size of the used variables or returns the true size of the struct.
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
AFAIK sizeof returns the total used size of the struct (which may include unused padding bytes etc.).
This is probably the most useful behaviour since sizeof is commonly used when allocating dynamic memory, and therefore it must report how much space a struct needs in practice, and not just it''s ''declared'' size.

This topic is closed to new replies.

Advertisement