Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Afterlife

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

This topic is 6173 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!