473,765 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_IN PUT) 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 4132
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_IN PUT) 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_IN PUT) 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_Keit h) 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_IN PUT) 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************ *********@o13g2 000cwo.googlegr oups.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_IN PUT) 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.l earn.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_IN PUT) returns 12. It should
return 10, shouldn't it?

sizeof(ULON G) = 4
sizeof(ULON G) = 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

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

Similar topics

3
2060
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 function displays the address of each of the 5 integers twice, first using the member pointer table "members" and then using an instance of myclass. The results are surprising: the values for x and y are incorrect when using the member pointers.
8
1851
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 test { int number;
4
2743
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
2812
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 free()? #include <stdlib.h> struct stype { int *foo; };
20
2642
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
26000
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
5311
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: does the following program terminates ? int main(void) { typedef struct t { unsigned char f; } t;
24
2811
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 bit processor ? Is there some problem with my compiler ? thanks for any help ...
2
1560
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 - 8 bytes) does anyone have any ideas why it occur?
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.