sizeof(4+4+1)==12 is incorrect!? 
Author Message
 sizeof(4+4+1)==12 is incorrect!?

Hi All,

-snip-
struct
{
 DWORD,
 DWORD,
bool} s1;
-snip-

Here is my struct which should calcutate to be 9 (4 + 4 + 1) but sizeof(s1)
becomes 12.
I was using this sizeof() (since i knew it is best to use sizeof than manual
calculations).
Now, I am forced to use contant number in fwrite since, this value is
different.

Please let me know what could be the problem here.

Thanks and Regards,
Vikas,IN



Thu, 03 Mar 2005 15:54:43 GMT  
 sizeof(4+4+1)==12 is incorrect!?

Quote:
> Here is my struct which should calcutate to be 9 (4 + 4 + 1) but
sizeof(s1)
> becomes 12.

This shouldn't bother you.  What difference does it make?

Quote:
> Now, I am forced to use contant number in fwrite since, this value is
> different.

Why are you forced to use a number rather than sizeof()?


Thu, 03 Mar 2005 16:04:38 GMT  
 sizeof(4+4+1)==12 is incorrect!?

Quote:

>Here is my struct which should calcutate to be 9 (4 + 4 + 1) but sizeof(s1)
>becomes 12.

I guess it's components are aligned on a DWORD boundary. Try setting
your compiler options to align on byte boundaries and see if you get a
different value.

--
Bob Moore [WinSDK MVP]
http://www.mooremvp.freeserve.co.uk/
(this is a non-commercial site and does not accept advertising)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do not reply via email unless specifically requested to do so.
Unsolicited email is NOT welcome and will go unanswered.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Thu, 03 Mar 2005 16:53:28 GMT  
 sizeof(4+4+1)==12 is incorrect!?

Quote:
>Please let me know what could be the problem here.

I'd assume it's a structure packing issue - unless your bool is
actually 4 bytes as Margaret suggests.

Try adding a #pragma pack directive around the structure definition to
ensure 1 byte packing.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq



Thu, 03 Mar 2005 17:08:29 GMT  
 sizeof(4+4+1)==12 is incorrect!?

Quote:

> I guess it's components are aligned on a DWORD boundary.

If that were the case, his sizeof would be 13, wouldn't it?  I suspect that
the compiler simply extends the size of the structure to avoid creating
efficiency issues.  For example, being able to express the struct in 3
32-bit quantities will make copying and passing the structure easier and
faster.

Quote:
> Try setting
> your compiler options to align on byte boundaries and see if you get a
> different value.

It shouldn't matter to him if his code is written correctly.


Thu, 03 Mar 2005 18:49:57 GMT  
 sizeof(4+4+1)==12 is incorrect!?
thanks a lot!!!
#pragma pack worked.

sizeof(bool)==1, and settings struct member aligment also could not help.

Thanks and Regards,
Vikas,IN


Quote:
> >Please let me know what could be the problem here.

> I'd assume it's a structure packing issue - unless your bool is
> actually 4 bytes as Margaret suggests.

> Try adding a #pragma pack directive around the structure definition to
> ensure 1 byte packing.

> Dave
> --
> MVP VC++ FAQ: http://www.mvps.org/vcfaq



Thu, 03 Mar 2005 19:21:09 GMT  
 sizeof(4+4+1)==12 is incorrect!?

Quote:

>For example, being able to express the struct in 3
>32-bit quantities will make copying and passing the structure easier and
>faster.

That's essentially what I was saying. If the compiler decides to align
all data on a quadword boundary, 4/4/1 data will end up occupying
4/4/4 space. The compiler reserves 4 bytes for the one byte item in
order to speed up accessing what comes _next_.

Quote:
>> Try setting
>> your compiler options to align on byte boundaries and see if you get a
>> different value.

>It shouldn't matter to him if his code is written correctly.

It matters a lot if you're transmitting data in structures to other
systems. A system I used to work on interfaced to other hardware with
firmware written in an ancient embedded C (Keil?) which didn't
understand anything larger than a byte, so structure packing was very
much an issue. You have to dig out the pragmas for situations like
that.

--
Bob Moore [WinSDK MVP]
http://www.mooremvp.freeserve.co.uk/
(this is a non-commercial site and does not accept advertising)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do not reply via email unless specifically requested to do so.
Unsolicited email is NOT welcome and will go unanswered.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Thu, 03 Mar 2005 20:42:40 GMT  
 sizeof(4+4+1)==12 is incorrect!?
Hi

there is an extra bit that vc adds to every structure. if u use a/stl its
not a problem. but as other people said, u can use #pragma pack

Daniel Cohen Gindi


Quote:
> Hi All,

> -snip-
> struct
> {
>  DWORD,
>  DWORD,
> bool} s1;
> -snip-

> Here is my struct which should calcutate to be 9 (4 + 4 + 1) but
sizeof(s1)
> becomes 12.
> I was using this sizeof() (since i knew it is best to use sizeof than
manual
> calculations).
> Now, I am forced to use contant number in fwrite since, this value is
> different.

> Please let me know what could be the problem here.

> Thanks and Regards,
> Vikas,IN



Thu, 03 Mar 2005 22:16:21 GMT  
 sizeof(4+4+1)==12 is incorrect!?

And also btw, those extra bytes are for protecting your data. in debug mode
there are more bytes surounding your data in the memory. in release mode if
u didnt turn of the checking for illegal memory operations such as writing
too much bytes and overwriting other heaps memory, so it will add you one
extra bytes.

Daniel Cohen Gindi


Quote:
> Hi All,

> -snip-
> struct
> {
>  DWORD,
>  DWORD,
> bool} s1;
> -snip-

> Here is my struct which should calcutate to be 9 (4 + 4 + 1) but
sizeof(s1)
> becomes 12.
> I was using this sizeof() (since i knew it is best to use sizeof than
manual
> calculations).
> Now, I am forced to use contant number in fwrite since, this value is
> different.

> Please let me know what could be the problem here.

> Thanks and Regards,
> Vikas,IN



Thu, 03 Mar 2005 22:19:40 GMT  
 sizeof(4+4+1)==12 is incorrect!?

Quote:
>And also btw, those extra bytes are for protecting your data. in debug mode
>there are more bytes surounding your data in the memory. in release mode if
>u didnt turn of the checking for illegal memory operations such as writing
>too much bytes and overwriting other heaps memory, so it will add you one
>extra bytes.

I think we're starting to get into computing myth, folklore, and
misinformation here! It's nothing of the sort.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq



Thu, 03 Mar 2005 22:45:16 GMT  
 sizeof(4+4+1)==12 is incorrect!?


Quote:

> >For example, being able to express the struct in 3
> >32-bit quantities will make copying and passing the structure easier and
> >faster.

> That's essentially what I was saying. If the compiler decides to align
> all data on a quadword boundary, 4/4/1 data will end up occupying
> 4/4/4 space. The compiler reserves 4 bytes for the one byte item in
> order to speed up accessing what comes _next_.

> >> Try setting
> >> your compiler options to align on byte boundaries and see if you get a
> >> different value.

> >It shouldn't matter to him if his code is written correctly.

> It matters a lot if you're transmitting data in structures to other
> systems. A system I used to work on interfaced to other hardware with
> firmware written in an ancient embedded C (Keil?) which didn't
> understand anything larger than a byte, so structure packing was very
> much an issue. You have to dig out the pragmas for situations like
> that.

Yes, I agree. This struct has to be frwitten and send on socket. the other
client will read it. and since it is a protocol, client could be on other
OS. Btw, such a problem also takes a lot of space, I am sending 700 or more
copies of such structs which amounts of 1400 bytes of usless data.


Fri, 04 Mar 2005 02:41:39 GMT  
 sizeof(4+4+1)==12 is incorrect!?

Quote:

>Hi All,

>-snip-
>struct
>{
> DWORD,
> DWORD,
>bool} s1;
>-snip-

>Here is my struct which should calcutate to be 9 (4 + 4 + 1) but sizeof(s1)
>becomes 12.
>I was using this sizeof() (since i knew it is best to use sizeof than manual
>calculations).
>Now, I am forced to use contant number in fwrite since, this value is
>different.

>Please let me know what could be the problem here.

Look up "Structure Alignment" in MSDN and #pragma pack. You might ask
why the compiler inserts padding at the end of your struct. One reason
is that you can have arrays of structs, and the compiler can't insert
padding between array elements. Thus, if the compiler didn't insert
the padding into your struct, there's no way it could respect the
alignment requirements of successive array elements.

The documentation also talks about the /Zp compiler option, which
affects the packing for entire source files and everything they
#include. Unless all the #included files use #pragma pack to
explicitly set their struct alignment, /Zp can cause your code to
apply a different alignment to these structs than was used to compile
other code (such as libraries) that use them. That's typically
disastrous, so don't use /Zp unless you're very sure of what you're
doing (and what everyone you #include is doing).

--
Doug Harrison
Microsoft MVP - Visual C++



Fri, 04 Mar 2005 05:09:25 GMT  
 sizeof(4+4+1)==12 is incorrect!?


Quote:
>Hi All,

>-snip-
>struct
>{
> DWORD,
> DWORD,
>bool} s1;
>-snip-

>Here is my struct which should calcutate to be 9 (4 + 4 + 1) but sizeof(s1)
>becomes 12.
>I was using this sizeof() (since i knew it is best to use sizeof than manual
>calculations).
>Now, I am forced to use contant number in fwrite since, this value is
>different.

>Please let me know what could be the problem here.

>Thanks and Regards,
>Vikas,IN

One of the requirements of the language is that if you have an array
of this struct, then element 1 of the array must be exactly
sizeof(struct) bytes beyond element 0.  Another requirement is that
each element be aligned properly.

DWORD apparently requires an alignment of 4.  There can be no padding
between the start of the struct and the first DWORD.  Therefore, the
struct must be aligned on a 4 byte boundary.  The physical minimum
size of the struct is 9 but this does not enforce the alignment
requirement.  The compiler is there forced to add padding either
before or after the bool to get to the next multiple of 4.

The basic rule is that sizeof(struct) will always be a multiple of the
most restrictive alignment of its elements.

<<Remove the del for email>>



Fri, 04 Mar 2005 06:55:10 GMT  
 sizeof(4+4+1)==12 is incorrect!?


Quote:

> > It matters a lot if you're transmitting data in structures to other
> > systems. A system I used to work on interfaced to other hardware with
> > firmware written in an ancient embedded C (Keil?) which didn't
> > understand anything larger than a byte, so structure packing was very
> > much an issue. You have to dig out the pragmas for situations like
> > that.

> Yes, I agree. This struct has to be frwitten and send on socket. the other
> client will read it. and since it is a protocol, client could be on other
> OS. Btw, such a problem also takes a lot of space, I am sending 700 or
more
> copies of such structs which amounts of 1400 bytes of usless data.

Well, in such a case, you should write a "send" function which takes your
structure as parameter and send its elements over the wire in the correct
order, with the proper alignement, binary representation and so on... and a
"receive" function to do the opposite.

But you should keep the "natural" internal representation chosen by the
compiler. #pragma pack'ing can be disastrous for performances, so it should
be avoided.

What is more, writing the "send" function makes the over-the-wire
representation of your data explicit and clear in your code, which isn't the
case if you rely on compiler switches, and it also insure portability.

Last advantage, if ever you decide to change your communication protocol
(say, to use XML), you simply needs to change the "send" and "receive"
functions.

Arnaud
MVP - VC



Fri, 04 Mar 2005 16:12:51 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. sizeof(4+4+1)==12 is incorrect!?

2. malloc(sizeof(char)*12)

3. OT Humor: The 12 Bugs of Christmas

4. VC 7.0 Linker Error LNK2005, LNK4006, LNK1169 _DllMain@12 already defined in MyDll.obj

5. console application takes 12 minutes to startup

6. link unresolved symbol __CorDLLMain@12

7. MC++ By passing constructor -- violation of section 12.7.3

8. 12 30 1899 - Why?

9. Help program giving EMM386 #12 error on single if >0

10. CONFERENCE: C&C++ European Developers Forum 98, Oxford, 11-12 September

11. Q20.12: Bit counting

12. 12+ Month Contract in Portland, Oregon

 

 
Powered by phpBB® Forum Software