Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Size of structs containing unions

Reply
Thread Tools

Size of structs containing unions

 
 
luke
Guest
Posts: n/a
 
      09-30-2005
hi,
in Visula C++ 6.0 I have declared a struct like this:

typedef struct _WRITE_INPUT {

ULONG DeviceNumber;
ULONG RegisterNumber;
union {
USHORT ShortData;
UCHAR CharData;
};
} WRITE_INPUT;

I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
return 10, shouldn't it?

sizeof(ULONG) = 4
sizeof(ULONG) = 4
sizeof(USHORT) = 2 (longest union field)

4 + 4 + 2 = 10

thanks
bye
luke

 
Reply With Quote
 
 
 
 
Anand
Guest
Posts: n/a
 
      09-30-2005
luke wrote:
> hi,
> in Visula C++ 6.0 I have declared a struct like this:
>
> typedef struct _WRITE_INPUT {
>
> ULONG DeviceNumber;
> ULONG RegisterNumber;
> union {
> USHORT ShortData;
> UCHAR CharData;
> };
> } WRITE_INPUT;
>
> I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
> return 10, shouldn't it?
>
> sizeof(ULONG) = 4
> sizeof(ULONG) = 4
> sizeof(USHORT) = 2 (longest union field)
>
> 4 + 4 + 2 = 10
>
> thanks
> bye
> luke
>

c.l.c FAQ #2.13 would answer your question
 
Reply With Quote
 
 
 
 
luke
Guest
Posts: n/a
 
      09-30-2005
thank you,
I thought it was a union problem
bye

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-30-2005
"luke" <> writes:
> in Visula C++ 6.0 I have declared a struct like this:
>
> typedef struct _WRITE_INPUT {


Don't use identifiers starting with an underscore; they're reserved to
the implementation. (It's slightly more complex than that, but it's
safest just to avoid them.)

> ULONG DeviceNumber;
> ULONG RegisterNumber;
> union {
> USHORT ShortData;
> UCHAR CharData;
> };
> } WRITE_INPUT;
>
> I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
> return 10, shouldn't it?
>
> sizeof(ULONG) = 4
> sizeof(ULONG) = 4
> sizeof(USHORT) = 2 (longest union field)
>
> 4 + 4 + 2 = 10


The compiler is free to add padding after any member of a structure.
In this case, it's probably adding 2 bytes of padding at the end to
make the size of the structure a multiple of 4, so the ULONG members
will be aligned properly if you have an array of structures.

Incidentally, the names ULONG, USHORT, and UCHAR aren't particularly
helpful. I presume they're typedefs (or macros?) for unsigned long,
unsigned short, and unsigned char, respectively. Why not just use the
names directly?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-30-2005
luke wrote:
> hi,
> in Visula C++ 6.0 I have declared a struct like this:
>
> typedef struct _WRITE_INPUT {
>
> ULONG DeviceNumber;
> ULONG RegisterNumber;
> union {
> USHORT ShortData;
> UCHAR CharData;
> };
> } WRITE_INPUT;
>
> I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
> return 10, shouldn't it?


Please check the FAQ before posting.
You might find <http://www.eskimo.com/~scs/C-faq/q2.13.html> enlightening.

BTW: Avoid nonstandard typenames in postings. If ULONG, USHORT, and
UCHAR are in fact unsigned long, unsigned short, and unsigned char, why
not use the real typenames?
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      09-30-2005
In article <. com>,
"luke" <> wrote:

> hi,
> in Visula C++ 6.0 I have declared a struct like this:
>
> typedef struct _WRITE_INPUT {
>
> ULONG DeviceNumber;
> ULONG RegisterNumber;
> union {
> USHORT ShortData;
> UCHAR CharData;
> };
> } WRITE_INPUT;


What is ULONG? What is USHORT? What is UCHAR?

> I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
> return 10, shouldn't it?
>
> sizeof(ULONG) = 4
> sizeof(ULONG) = 4
> sizeof(USHORT) = 2 (longest union field)
>
> 4 + 4 + 2 = 10


Compilers usually add padding (unused bytes) between members of a struct
or at the end of a struct to align the data in the struct and to allow
the processor to access them faster.

If one member of a struct has a size of four bytes, then quite often the
size of the struct will be increased to a multiple of four bytes, if
necessary.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-01-2005
On 30 Sep 2005 00:23:18 -0700, "luke" <> wrote in
comp.lang.c:

> hi,
> in Visula C++ 6.0 I have declared a struct like this:


Aside from the other replies you received, your code is not C at all,
and does not belong in this newsgroup in any way, shape, or form.

>
> typedef struct _WRITE_INPUT {
>
> ULONG DeviceNumber;
> ULONG RegisterNumber;
> union {
> USHORT ShortData;
> UCHAR CharData;
> };


There is no such thing as an anonymous union in C. A conforming C
compiler must issue a diagnostic for this.

Although it appears that Visual C++ 6.0 is non-conforming in this
respect.

> } WRITE_INPUT;


[snip]

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Ravi Uday
Guest
Posts: n/a
 
      10-14-2005

Keith Thompson wrote:

> "luke" <> writes:
>
>>in Visula C++ 6.0 I have declared a struct like this:
>>
>>typedef struct _WRITE_INPUT {

>
>
> Don't use identifiers starting with an underscore; they're reserved to
> the implementation. (It's slightly more complex than that, but it's
> safest just to avoid them.)
>
>

sorry to be nitty, but since some of us here do work at the OS/kernel
level and use C, I think using underscore when declaring objects are
fine (it would be a part of bug fix ) ?

- Ravi


>> ULONG DeviceNumber;
>> ULONG RegisterNumber;
>> union {
>> USHORT ShortData;
>> UCHAR CharData;
>> };
>>} WRITE_INPUT;
>>
>>I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
>>return 10, shouldn't it?
>>
>>sizeof(ULONG) = 4
>>sizeof(ULONG) = 4
>>sizeof(USHORT) = 2 (longest union field)
>>
>>4 + 4 + 2 = 10

>
>
> The compiler is free to add padding after any member of a structure.
> In this case, it's probably adding 2 bytes of padding at the end to
> make the size of the structure a multiple of 4, so the ULONG members
> will be aligned properly if you have an array of structures.
>
> Incidentally, the names ULONG, USHORT, and UCHAR aren't particularly
> helpful. I presume they're typedefs (or macros?) for unsigned long,
> unsigned short, and unsigned char, respectively. Why not just use the
> names directly?
>


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      10-14-2005
Ravi Uday a écrit :
>
> Keith Thompson wrote:
>
>> "luke" <> writes:
>>
>>> in Visula C++ 6.0 I have declared a struct like this:
>>>
>>> typedef struct _WRITE_INPUT {

>>
>> Don't use identifiers starting with an underscore; they're reserved to
>> the implementation. (It's slightly more complex than that, but it's
>> safest just to avoid them.)
>>

> sorry to be nitty, but since some of us here do work at the OS/kernel
> level and use C, I think using underscore when declaring objects are
> fine (it would be a part of bug fix ) ?


Implementation means compiler and compiler's libraries. Linux Kernel
Modules or the like are part of the system, not of the implmentation.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-14-2005
Emmanuel Delahaye wrote:

> Implementation means compiler and compiler's libraries. Linux Kernel
> Modules or the like are part of the system, not of the implmentation.


I think "implementation" means everything, including hardware.
If you change the system, then you change the implementation.

N869
3.10
[#1] implementation
a particular set of software, running in a particular
translation environment under particular control options,
that performs translation of programs for, and supports
execution of functions in, a particular execution
environment

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fcntl() and packing structs with unions. tyler Python 0 09-04-2006 08:29 AM
unions & structs... chump1708@yahoo.com C Programming 18 01-18-2006 05:44 PM
The size of structs containing "int a[0]"? Lokicer C Programming 4 10-16-2005 06:00 PM
C++ member pointers and anonymous structs/unions broken? Udo Steinberg C++ 3 05-15-2004 09:19 PM
curiosity: structure assignments in C (and unions within structs) Neil Zanella C Programming 1 09-17-2003 04:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57