473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Size of structs containing unions

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

Nov 15 '05 #1
13 4102
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
Nov 15 '05 #2
thank you,
I thought it was a union problem
bye

Nov 15 '05 #3
"luke" <lr*****@yahoo.com> 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) ks***@mib.org <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.
Nov 15 '05 #4
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?
Nov 15 '05 #5
In article <11*********************@o13g2000cwo.googlegroups. com>,
"luke" <lr*****@yahoo.com> 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.
Nov 15 '05 #6
On 30 Sep 2005 00:23:18 -0700, "luke" <lr*****@yahoo.com> 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
Nov 15 '05 #7

Keith Thompson wrote:
"luke" <lr*****@yahoo.com> 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?


Nov 15 '05 #8
Ravi Uday a écrit :

Keith Thompson wrote:
"luke" <lr*****@yahoo.com> 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.
Nov 15 '05 #9
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
Nov 15 '05 #10
On Fri, 14 Oct 2005 17:10:07 +0530, Ravi Uday <ra******@gmail.com>
wrote:

Keith Thompson wrote:
"luke" <lr*****@yahoo.com> 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 ) ?


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

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #11
On Fri, 14 Oct 2005 15:01:00 GMT, pete <pf*****@mindspring.com> wrote:
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


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.
--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #12
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.

Nov 15 '05 #13
Alan Balmer wrote:

On Fri, 14 Oct 2005 15:01:00 GMT, pete <pf*****@mindspring.com> wrote:
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


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

--
pete
Nov 15 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Udo Steinberg | last post by:
Hi, The program below stores the 5 unsigned ints a, b, c, x and y in an anonymous union, so that they share storage space with state. Additionally x and y share storage space with nums. The main...
8
by: Bryan Feeney | last post by:
This structure is, according to sizeof, 3 bytes long, which makes sense struct test { char text; }; This structure is, according to sizeof, 4 bytes long, which also makes sense struct...
4
by: Lokicer | last post by:
Hi, i am a c newbie, i write some code to get size of structs i compile and run it in VC6.0 //#pragma pack(1) typedef struct tag_NullMsg { int a; } tNullMsg; typedef struct tag_CharMsg
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
12
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
7
by: Francois Grieu | last post by:
Hello, is the size of a type, defined using typedef as a collection of arrays of unsigned char, the sum of the size of the arrays ? After simplifying my question, it probably is equivalent to:...
24
by: junky_fellow | last post by:
Hi, I am using a gcc compiler for a 32 bit powerpc processor. When I look at the size of unsigned long long, it displays 8 bytes. I was wondering, how we can have a data type of 64 bits on a 32...
2
by: guy.gorodish | last post by:
hi, i have a struct in c# that is containing Int32 member and Double member. when i try to get the size of it i get size of 16 bytes, while i was expecting to receive 12. (Int32- 4 bytes, Double...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.