I was just thumbing through K&R 2nd Edition and thought I'd read up on
bit-fields, which I [personally] haven't had much use for in my career.
Anyhow, K&R says that, "Fields may be declared only as ints; for
portability, specify signed or unsigned explicitly". So it got me
wondering what would happen if I tried to declare a field as a short
rather than an int.
I compiled my code with GCC 3.3.6 with all warning turned on and I got
no warnings returned. Have things changed since K&R or is there
something bad about my code?
On my implementation, the sizeof() each bit-field is identical to the
type I used to declare the fields.
<code>
#include <stdio.h>
struct {
unsigned short admin : 1;
unsigned short manager : 1;
unsigned short supervisor : 1;
unsigned short user : 1;
unsigned short guest : 1;
} roles;
struct {
unsigned int admin : 1;
unsigned int manager : 1;
unsigned int supervisor : 1;
unsigned int user : 1;
unsigned int guest : 1;
} roles2;
int main(void)
{
printf("sizeof(short): %d sizeof(roles): %d\n",
sizeof(short), sizeof(roles));
printf("sizeof(int): %d sizeof(roles2): %d\n",
sizeof(int), sizeof(int));
return 0;
}
</code>
Just curious...
Thank you in advance,
--
Sean 5 2297
"Fao, Sean" <en**********@yahoo.comI-WANT-NO-SPAM> writes: I was just thumbing through K&R 2nd Edition and thought I'd read up on bit-fields, which I [personally] haven't had much use for in my career. Anyhow, K&R says that, "Fields may be declared only as ints; for portability, specify signed or unsigned explicitly". So it got me wondering what would happen if I tried to declare a field as a short rather than an int.
What will happen depends on the implementation. The standard says
(C99 6.7.2.1p4):
A bit-field shall have a type that is a qualified or unqualified
version of _Bool, signed int, unsigned int, or some other
implementation-defined type.
An implementation may support other types for bit fields, but it's not
required to; any program that uses such an extension is non-portable.
I compiled my code with GCC 3.3.6 with all warning turned on and I got no warnings returned. Have things changed since K&R or is there something bad about my code?
On my implementation, the sizeof() each bit-field is identical to the type I used to declare the fields.
C99 6.5.3.4p1:
The sizeof operator shall not be applied to an expression that has
function type or an incomplete type, to the parenthesized name of
such a type, or to an expression that designates a bit-field
member.
This is a constraint, so if gcc doesn't give you a diagnostic, it's
non-conforming. (gcc can generally be persuaded to be conforming, or
nearly so, with command-line options.)
--
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.
On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
[snip] On my implementation, the sizeof() each bit-field is identical to the type I used to declare the fields. C99 6.5.3.4p1:
The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.
This is a constraint, so if gcc doesn't give you a diagnostic, it's non-conforming. (gcc can generally be persuaded to be conforming, or nearly so, with command-line options.)
You missed on this last one, Keith. His remark, which you are
commenting on here, is inaccurate and does not reflect what his code
actually does. I have replaced his code below, which you snipped:
#include <stdio.h>
struct { unsigned short admin : 1; unsigned short manager : 1; unsigned short supervisor : 1; unsigned short user : 1; unsigned short guest : 1; } roles;
struct { unsigned int admin : 1; unsigned int manager : 1; unsigned int supervisor : 1; unsigned int user : 1; unsigned int guest : 1; } roles2;
int main(void) { printf("sizeof(short): %d sizeof(roles): %d\n", sizeof(short), sizeof(roles)); printf("sizeof(int): %d sizeof(roles2): %d\n", sizeof(int), sizeof(int));
return 0; }
Clearly he is not applying the sizeof operator to the bit-field
members, but to the entire struct type containing the bit-field
members, and this of course is perfectly valid.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
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
Jack Klein <ja*******@spamcop.net> writes: On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <ks***@mib.org> wrote in comp.lang.c:
[snip]
> On my implementation, the sizeof() each bit-field is identical to the > type I used to declare the fields. C99 6.5.3.4p1:
The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.
This is a constraint, so if gcc doesn't give you a diagnostic, it's non-conforming. (gcc can generally be persuaded to be conforming, or nearly so, with command-line options.)
You missed on this last one, Keith. His remark, which you are commenting on here, is inaccurate and does not reflect what his code actually does. I have replaced his code below, which you snipped:
[snip again]
Clearly he is not applying the sizeof operator to the bit-field members, but to the entire struct type containing the bit-field members, and this of course is perfectly valid.
Whoops.
--
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.
<en**********@yahoo.comI-WANT-NO-SPAM> wrote: ... ... int main(void) { printf("sizeof(short): %d sizeof(roles): %d\n", sizeof(short), sizeof(roles)); printf("sizeof(int): %d sizeof(roles2): %d\n", sizeof(int), sizeof(int));
-------------------------------^^^
You probably meant "roles2" here.
Roberto Waltman
[ Please reply to the group,
return address is invalid ]
Roberto Waltman <us****@rwaltman.net> writes: <en**********@yahoo.comI-WANT-NO-SPAM> wrote:... ... int main(void) { printf("sizeof(short): %d sizeof(roles): %d\n", sizeof(short), sizeof(roles)); printf("sizeof(int): %d sizeof(roles2): %d\n", sizeof(int), sizeof(int)); -------------------------------^^^
You probably meant "roles2" here.
Apart from that, "%d" isn't a correct format for size_t, which is the
type that the sizeof operator yields. Either use "%ld" and cast the
value to unsigned long, or use the C99-specific "%zd" format.
--
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bonj |
last post by:
Happy new year to everybody...
Just a few questions about porting code to 64-bit environment.
1) Is there a different version of the SDK (i.e., C/C++ compiler) for the
64-bit platform, or does...
|
by: Amy DBA |
last post by:
I've been asked to administer a DB2 V 8 (32-bit install) on a Solaris
64-bit platform. It seems like whomever installed DB2 on the server,
goofed for not installing DB2 v8 64 bit. Do I understand...
|
by: Jean-Marc Blaise |
last post by:
Hi,
Is it worth to use 64-bit DB2 instances on a 32-bit kernel, in terms of:
- performance
- configuration (go beyond the 256 Mb segment for private mem, 1.75 Gb for
Bufferpools)
- other ?
...
|
by: Mohanasundaram |
last post by:
Hi All,
We are working on porting a product written in C and C++ from 32 bit
to 64 bit. We need to maintain both 32 bit and 64 bit versions in the
future. We took the 32 bit source code and...
|
by: Henry Samuelson |
last post by:
I am looking for some bit wizardy for the following:
We have two 32-bit integers A and B. I need to come up with an operation
O that takes A and B as arguments, spitting out a single integer R...
|
by: JDeats |
last post by:
1. Will there be different 64-bit .NET implementations for Intel and AMD
64-bit processors or will they share a common 64-bit CLR?
2. Will .NET managed code compiled for the 32-bit CLR be binary...
|
by: Ganapathy |
last post by:
I have COM dll code written in VC 6.0. When i tried
compiling this code in VC 7, The MIDL cmpiler gets called
twice. i.e. it initially compiles fully & immediately a
line - 64 bit processing'...
|
by: ern |
last post by:
Does a function exist to convert a 128-bit hex number to a string?
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
I am using C# to develop DLL using Visual Studio 2005 and .Net 2.0, and I
have no idea of how to make my DLL work with applications on 64-bit platform.
Above all, I do not...
|
by: Chuck Chopp |
last post by:
I have some code that is being built on the following:
Windows Server 2003, both 32-bit & 64-bit editions
Windows Vista, both 32-bit & 64-bit editions
Windows Server 2008, both 32-bit & 64-bit...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
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: 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...
| |