Size of structs containing unions

L

luke

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
 
A

Anand

luke said:
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
 
K

Keith Thompson

luke said:
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?
 
M

Martin Ambuhl

luke said:
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?
 
C

Christian Bau

"luke said:
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.
 
J

Jack Klein

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]
 
R

Ravi Uday

Keith said:
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
 
E

Emmanuel Delahaye

Ravi Uday a écrit :
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.
 
P

pete

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
 
A

Alan Balmer

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 ) ?

Well, no. OS/kernel is not part of the C implementation. However, if
the system APIs you're working with use identifiers beginning with
underscore, you're stuck with it, for all practical purposes. I just
hope other aspects of the API don't show the same ignorance of
standards.

BTW, do you think the OP is really doing OS/kernel work using Visual
C++ 6.0?
 
A

Alan Balmer

Emmanuel said:
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

Re-read the reference. "A particular set of software" is the subject
of the sentence. The rest is description of that set of software.

In the sense of the standard, the hardware is not part of the
implementation, though the implementation can support a particular set
of hardware.
 
V

vivek.sriwastava

There is concept of structure padding. it depends on compiler to
compiler , when u use the turbo C compiler u will find the size as 10
but VC++ always use structure padding that is why it is 12.
Some compilers usually use the space in the multiple of even nos like
2,4,or 8..
if the size of structure is not multiple of predecided then it add the
required no of bytes.
 
P

pete

Alan said:
Emmanuel said:
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

Re-read the reference. "A particular set of software" is the subject
of the sentence. The rest is description of that set of software.

In the sense of the standard, the hardware is not part of the
implementation, though the implementation can support a particular set
of hardware.

Thank you.
What is an "environment"
as in "translation environment" and "execution environment"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top