473,695 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Questions on bitfields and struct

Hi,

1. How it that the results for the size of struct1 and struct2 (below)
are 4 and 3

# include <stdio.h>

struct struct1
{
const :16;
volatile :4;
};

struct struct2
{
int :1;
unsigned :1;
const :16;
volatile :4;
};

int main()
{
printf ("Size of struct 1 = %d\n",sizeof(st ruct struct1)); /*
Prints 4 */
printf ("Size of struct 2 = %d\n",sizeof(st ruct struct2)); /*
Prints 3 */

}

2. Also what is meant by "incomplete type" in C?
3. What does tag refer to in a struct/union declaration?

Regards,
Sarathy

Jul 20 '06 #1
7 1959
In article <11************ **********@p79g 2000cwp.googleg roups.com>,
sarathy <sp*********@gm ail.comwrote:
>1. How it that the results for the size of struct1 and struct2 (below)
are 4 and 3
>struct struct1
{
const :16;
volatile :4;
};
>struct struct2
{
int :1;
unsigned :1;
const :16;
volatile :4;
};
With SGI's IRIX cc compiler, both are 3. With gcc 3.3 on the same
machine, the first is 4 and the second is 3. Which is to say that the
alignment mechanisms are implementation dependant, and are not required
to be consistant.

It is, for example, entirely legal for the compiler to examine the
first structure, note that the first field is the same size as a short
int on that implementation, and decide that the structure shall be
built for fast access as a pair of shorts aligned on a short boundary,
for a total of 4 bytes.

The same compiler could look at the second structure and decide that it
is complicated enough that space is the important factor rather than
speed, and decide to pull bits out of 3 bytes.

But compilers are not required to allow bitfields to cross word
boundaries, so the compiler -could- have decided to put the first two
fields into one short, the third field into a second short, and the
fourth field into a third short, for a total of 6 bytes. Or it could
have decided that since the total fits within 32 bits that it would
pull bits out of a long, for a total of 4 bytes.

Decisions about when to move to the next word and how much padding
to use before that word are completely up to the compiler: the
closest that the C standard comes on this point is to say "if it fits".

>2. Also what is meant by "incomplete type" in C?
The C faq probably talks about that in better words than I could
come up with in a reasonable time.

>3. What does tag refer to in a struct/union declaration?
A structure tag is a programmer-given name for that variety of
structure. It is not a variable, but rather a reference to the type.
As a rough analogy: "Form 37/J" might be a name given to a particular
layout of income tax form, but "Form 37/J" is not a particular -copy-
of the form, it is the name of the -kind- of form.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jul 20 '06 #2

Walter Roberson wrote:
In article <11************ **********@p79g 2000cwp.googleg roups.com>,
sarathy <sp*********@gm ail.comwrote:
1. How it that the results for the size of struct1 and struct2 (below)
are 4 and 3
struct struct1
{
const :16;
volatile :4;
};
struct struct2
{
int :1;
unsigned :1;
const :16;
volatile :4;
};

With SGI's IRIX cc compiler, both are 3. With gcc 3.3 on the same
machine, the first is 4 and the second is 3. Which is to say that the
alignment mechanisms are implementation dependant, and are not required
to be consistant.

It is, for example, entirely legal for the compiler to examine the
first structure, note that the first field is the same size as a short
int on that implementation, and decide that the structure shall be
built for fast access as a pair of shorts aligned on a short boundary,
for a total of 4 bytes.

The same compiler could look at the second structure and decide that it
is complicated enough that space is the important factor rather than
speed, and decide to pull bits out of 3 bytes.

But compilers are not required to allow bitfields to cross word
boundaries, so the compiler -could- have decided to put the first two
fields into one short, the third field into a second short, and the
fourth field into a third short, for a total of 6 bytes. Or it could
have decided that since the total fits within 32 bits that it would
pull bits out of a long, for a total of 4 bytes.

Decisions about when to move to the next word and how much padding
to use before that word are completely up to the compiler: the
closest that the C standard comes on this point is to say "if it fits".

2. Also what is meant by "incomplete type" in C?

The C faq probably talks about that in better words than I could
come up with in a reasonable time.

3. What does tag refer to in a struct/union declaration?

A structure tag is a programmer-given name for that variety of
structure. It is not a variable, but rather a reference to the type.
As a rough analogy: "Form 37/J" might be a name given to a particular
layout of income tax form, but "Form 37/J" is not a particular -copy-
of the form, it is the name of the -kind- of form.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Thanks a lot. I got the things clear now. The next fun starts...
Consider the code

struct struct1
{
short a1:16;
short b1:4;
};

struct struct2
{
int a2:16;
unsigned b2:1;
short c2 :16;
short d2 :4;
};

int main()
{
struct struct1 *str1=(struct struct1 *)malloc(sizeof (struct
struct1));
str1->a1=24;
str1->b1=4;
print_bits(str1 ,sizeof(str1));
printf ("Size of struct1 = %d\n",sizeof(st ruct struct1));
/* Prints 4 */
printf ("Size of str1 = %d\n",sizeof(st r1)); /* Prints 4
*/

struct struct2 *str2=(struct struct2 *)malloc(sizeof (struct
struct2));
str2->a2=1;
str2->b2=1;
str2->c2=6;
str2->d2=12;
print_bits(str2 ,sizeof(struct struct2));
printf ("Size of struct2 = %d\n",sizeof(st ruct struct2));
/* Prints 8 */
printf ("Size of str2 = %d\n",sizeof(st r2)); /* Prints 4
*/
}
Output:
----------

00000000 00000100 00000000 00011000
Size of struct1 = 4
Size of str1 = 4
00000000 00001100 00000000 00000110 00000000 00000001 00000000 00000001
Size of struct2 = 8
Size of str2 = 4

Is there any difference in sizeof (struct tag_name) and
sizeof(struct_i nstance_identif ier)
In the third case, clearly from the results, 8 is correct due to
padding. But why is 4 being printed in the last printf.

Jul 20 '06 #3
In article <11************ *********@p79g2 000cwp.googlegr oups.com>,
sarathy <sp*********@gm ail.comwrote:
>
Walter Roberson wrote:
[lots]

Please trim down quotations to just the portion needed for discussion.

>Consider the code
struct struct2 *str2=(struct struct2 *)malloc(sizeof (struct
struct2));
printf ("Size of str2 = %d\n",sizeof(st r2)); /* Prints 4
*/
>Size of str2 = 4
>Is there any difference in sizeof (struct tag_name) and
sizeof(struct_ instance_identi fier)
No.

>In the third case, clearly from the results, 8 is correct due to
padding. But why is 4 being printed in the last printf.
Because str2 is not an instance of struct struct2.
str2 is a *pointer* to an instance of struct struct2.
sizeof(str2) is printing out the size of the pointer.

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Jul 20 '06 #4
Hi,
Does the 4 imply that the machine is 32 bit using 32 bit
addressing?
Does this have any impact on wordlength of the machine, because

in Turbo C in Windows(DOS environment) int --16 bit == address
length
in gcc in i386 Linux int --32 bits == address length

Regards,
Sarathy

Jul 22 '06 #5
On 22 Jul 2006 00:18:47 -0700, "sarathy" <sp*********@gm ail.com>
wrote:
>Hi,
Does the 4 imply that the machine is 32 bit using 32 bit
addressing?
I give up - what 4?
Does this have any impact on wordlength of the machine, because
And what this?
>
in Turbo C in Windows(DOS environment) int --16 bit == address
length
in gcc in i386 Linux int --32 bits == address length
We normally avoid issues regarding specific compilers and operating
systems. There are groups where those subjects are topical.
Remove del for email
Jul 22 '06 #6
On 22 Jul 2006 00:18:47 -0700, in comp.lang.c , "sarathy"
<sp*********@gm ail.comwrote:
>Hi,
Does the 4 imply that the machine is 32 bit using 32 bit
addressing?
Which 4?

--
Please quote enough of the previous message for context. Even google
now makes this quite easy.

Jul 22 '06 #7
On 2006-07-22, sarathy <sp*********@gm ail.comwrote:
Does the 4 imply that the machine is 32 bit using 32 bit addressing?
What 4 are you talking about?
Does this have any impact on wordlength of the machine, because in
Turbo C in Windows(DOS environment) int --16 bit == address length
C has no concept of "wordlength ".
in gcc in i386 Linux int --32 bits == address length
This is true to the best of my knowledge, but your other questions make
me wonder how you arrived at that figure. Not by multiplying anything by
8, right?

--
Andrew Poelstra <website down>
My server is down; you can't mail
me, nor can I post convieniently.
Jul 22 '06 #8

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

Similar topics

3
3398
by: Jon Slaughter | last post by:
I'm using bit fields to compactly represent some data I need to manage and I've read that they are not portable. I don't understand why that is the case? As long as I don't "indirectly" mess with the fields and I go through the compiler and allow it to choose the appropriate way to handle them then why shouldn't it be ok? The whole point of me using bit fields was so that I could be somewhat portable instead of managing the bits...
0
400
by: tmartsum | last post by:
I have a discussion in comp.std.c++ After a (bit stupid) suggestion on representing a fixed 'big' length int I moderated it to "Bitfields-ints should be allowed to have any fixed length" I got no replys on my moderated suggestion. (I am asking you your opinion of this - I do not really have a C++-problem)
23
2821
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find relevance.Though both of these(bitfields and unions) are used where space is a constraint(so I can assume always in embedded systems,where memory is particularly less)and we want to save space/memory. As far as I have read, there is no...
6
2680
by: GalenTX | last post by:
I am looking for opinions on a possible approach to coping with bitfields in a heterogenous environment. We've got a bunch of legacy code which maps to hardware using bitfields. The code was developed for a PowerPC/VxWorks/Tornado enviromant, with the bits packed in a big-endian manner: msb first. We now have to develop an Intel/Windows/MSVC++ version (lsb assigned first) which must maintain compatibility with the hardware interface...
19
14805
by: Mehta Shailendrakumar | last post by:
Hi, I would like to know why array of bitfields is not possible. Is there any relation with processor architecture for this? Thank you for your time. Regards, Shailendra
2
8133
by: Jason Curl | last post by:
Hello C Group, From what I remember, it is implementation dependent the way that a compiler may order bitfields in a struct. In the example program I give, Solaris Sparc gives one result, Intel x86 gives another result (for the GCC compiler). Is there a portable way to test how this information is stored? In particular, will the result of the program below always result in c being 0x01 or 0x80? Does the compiler allow for padding so...
3
1787
by: TedKennedyMurderedHisPregnantMistress.dwpj65 | last post by:
Hello, I'm attempting to define a class that has a bitfield as a static member. The definition seems ok, but I'm at a loss as to how to declare it: class Test { static struct TestBitFields { unsigned int Flag1:1; unsigned int Flag2:1;
9
19254
by: cman | last post by:
Who can explain to me what bitfields are and how to use them in practice? I would need a fairly detailed explaination, I would be a newbie to advanced C programming features. What are the advantages of using bitfields? cman
10
8492
by: lithiumcat | last post by:
Hi, This question seems to come up quite often, however I haven't managed to find an answer relevant to my case. I often use binary flags, and I have alaways used a "bitmask" technique, that is using #defined or const int powers of two, and using the following primitives : /* declaration and initizalisation of flags */ int flags = 0; /* setting flag */ flags |= FLAG_1;
0
8649
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
8586
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
9137
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
7683
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...
1
6506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
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
2
2289
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1986
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.