Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > granularity of size of operator

Reply
Thread Tools

granularity of size of operator

 
 
chinu
Guest
Posts: n/a
 
      08-04-2006
mail
hi all..

is it possible to find the size of a structure upto bit size.., where
the structure contains only one element and that too with bit
specifier..eg..

struct x {
char a : 4;
} y;

int main(){

printf("size of struct is %d\n",sizoef(y)); //will give 1
}

is there any way to get the size exactly upto no of bits.. so for the
ex above.. size should be 4..., i can disable ths automatic stuffing by
pragma.. still the smallest granularity for pragma is 1 byte only..
-- i think compiler should be able to tell this thing.., it is just
matter of asking the compiler right thing.. he wh

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      08-04-2006
chinu a écrit :
> mail
> hi all..
>
> is it possible to find the size of a structure upto bit size.., where
> the structure contains only one element and that too with bit
> specifier..eg..
>
> struct x {
> char a : 4;
> } y;
>
> int main(){
>
> printf("size of struct is %d\n",sizoef(y)); //will give 1
> }
>
> is there any way to get the size exactly upto no of bits.. so for the
> ex above.. size should be 4..., i can disable ths automatic stuffing by
> pragma.. still the smallest granularity for pragma is 1 byte only..
> -- i think compiler should be able to tell this thing.., it is just
> matter of asking the compiler right thing.. he wh
>


The processor you use can address bits?

The smallest addressable unit in most processors I know
is 1 byte. Period.

Since you can't address less, there is no point in having sizeof
return bit sizes.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-04-2006
"chinu" <(E-Mail Removed)> writes:
> is it possible to find the size of a structure upto bit size.., where
> the structure contains only one element and that too with bit
> specifier..eg..
>
> struct x {
> char a : 4;
> } y;
>
> int main(){
>
> printf("size of struct is %d\n",sizoef(y)); //will give 1
> }
>
> is there any way to get the size exactly upto no of bits.. so for the
> ex above.. size should be 4..., i can disable ths automatic stuffing by
> pragma.. still the smallest granularity for pragma is 1 byte only..
> -- i think compiler should be able to tell this thing.., it is just
> matter of asking the compiler right thing.. he wh


The size of any object (other than a bit field) is always a whole
number of bytes.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      08-04-2006
jacob navia wrote:
> chinu a écrit :
> > mail
> > hi all..
> >
> > is it possible to find the size of a structure upto bit size.., where
> > the structure contains only one element and that too with bit
> > specifier..eg..
> >
> > struct x {
> > char a : 4;
> > } y;
> >
> > int main(){
> >
> > printf("size of struct is %d\n",sizoef(y)); //will give 1
> > }
> >
> > is there any way to get the size exactly upto no of bits.. so for the
> > ex above.. size should be 4..., i can disable ths automatic stuffing by
> > pragma.. still the smallest granularity for pragma is 1 byte only..
> > -- i think compiler should be able to tell this thing.., it is just
> > matter of asking the compiler right thing.. he wh
> >

>
> The processor you use can address bits?
>
> The smallest addressable unit in most processors I know
> is 1 byte. Period.


The Microchip PIC can address bits and most PIC C compilers have
extensions to do bit addressing though not the way the OP's written it.
But yeah, no implementation of C I know can malloc or sizeof bits.

 
Reply With Quote
 
chinu
Guest
Posts: n/a
 
      08-04-2006

jacob navia wrote:

> chinu a écrit :
> > mail
> > hi all..
> >
> > is it possible to find the size of a structure upto bit size.., where
> > the structure contains only one element and that too with bit
> > specifier..eg..
> >
> > struct x {
> > char a : 4;
> > } y;
> >
> > int main(){
> >
> > printf("size of struct is %d\n",sizoef(y)); //will give 1
> > }
> >
> > is there any way to get the size exactly upto no of bits.. so for the
> > ex above.. size should be 4..., i can disable ths automatic stuffing by
> > pragma.. still the smallest granularity for pragma is 1 byte only..
> > -- i think compiler should be able to tell this thing.., it is just
> > matter of asking the compiler right thing.. he wh
> >

>
> The processor you use can address bits?
>

no, its not about addressing the bits individually.

> The smallest addressable unit in most processors I know
> is 1 byte. Period.
>

same is true for me..)
> Since you can't address less, there is no point in having sizeof


point comes in different context.. it would be helpful in embedded
system env.. where i need to know the size of individual bits to work
on them only.., othervise i need to remember the width of each field..



> return bit sizes.


 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      08-04-2006
jacob navia <(E-Mail Removed)> writes:

> chinu a écrit :
>> mail
>> hi all..
>> is it possible to find the size of a structure upto bit size.., where
>> the structure contains only one element and that too with bit
>> specifier..eg..
>> struct x {
>> char a : 4;
>> } y;
>> int main(){
>> printf("size of struct is %d\n",sizoef(y)); //will give 1
>> }
>> is there any way to get the size exactly upto no of bits.. so for the
>> ex above.. size should be 4..., i can disable ths automatic stuffing by
>> pragma.. still the smallest granularity for pragma is 1 byte only..
>> -- i think compiler should be able to tell this thing.., it is just
>> matter of asking the compiler right thing.. he wh
>>

>
> The processor you use can address bits?
>


He didnt ask to address the bits. He asked to find the size in bits of a bitfield.
 
Reply With Quote
 
goose
Guest
Posts: n/a
 
      08-04-2006
chinu wrote:
> jacob navia wrote:
>


<snipped>

> > The processor you use can address bits?
> >

> no, its not about addressing the bits individually.


I'd be interested to know what you plan to do with
the size in bits.

>
> > The smallest addressable unit in most processors I know
> > is 1 byte. Period.
> >

> same is true for me..)
> > Since you can't address less, there is no point in having sizeof

>
> point comes in different context.. it would be helpful in embedded
> system env.. where i need to know the size of individual bits to work
> on them only.., othervise i need to remember the width of each field..


I *am* in embedded environments, but I have yet
yearn for a sizeof that returns the number
of bits of a type or variable.

Anyway, since the standard defines sizeof (char) to be
1, what do you expect to get for a datatype less than
1 byte in size? 0.5?

goose,

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      08-04-2006
chinu wrote:
> mail
> hi all..
>
> is it possible to find the size of a structure upto bit size.., where
> the structure contains only one element and that too with bit
> specifier..eg..
>
> struct x {
> char a : 4;
> } y;
>
> int main(){
>
> printf("size of struct is %d\n",sizoef(y)); //will give 1
> }


#include <stdio.h>
#include <limits.h>

struct x
{
unsigned a:4; /* declaring a bit field as type char
is not part of C */
} y;

int main()
{
printf("[output for this implementation]\n"
"size of struct is %u bytes, %u bits\n",
(unsigned) sizeof y, (unsigned) (CHAR_BIT * sizeof y));
return 0;
}

[output for this implementation]
size of struct is 4 bytes, 32 bits

> is there any way to get the size exactly upto no of bits.. so for the
> ex above.. size should be 4...,


Where did you get such a silly idea? You will note that your comment
"//will give 1" is also untrue for the implementation on which I ran the
above program.

> i can disable ths automatic stuffing by
> pragma.. still the smallest granularity for pragma is 1 byte only..


In what units are addresses specified on your machine? Can you really
expect an array of 'struct x's to be properly aligned for access _and_
have each one only 4 bits long? Actually, those are rhetorical
questions, since a C program treats the smallest addressable unit as a
char, which must be at least 8 bits wide, no matter what the underlying
hardware might do.

> -- i think compiler should be able to tell this thing..,


Tell what thing? Your mental states are not available to the compiler.
The size of that struct in either bytes or bits is not a reflection of
your mental state, and the compiler should not have to probe your brain
to find the answers.

> it is just
> matter of asking the compiler right thing.. he wh


It is just a matter of knowing what questions make sense to ask and what
expectations it makes sense to hold.


 
Reply With Quote
 
Haroon Shafiq
Guest
Posts: n/a
 
      08-04-2006

Martin Ambuhl wrote:
> chinu wrote:


[...]

> > is it possible to find the size of a structure upto bit size.., where
> > the structure contains only one element and that too with bit
> > specifier..eg..


I think what OP wants is a way to know how many bits a field _pretends_
to span _not_ what the actual size of the structure or the fields is.

> > is there any way to get the size exactly upto no of bits.. so for the
> > ex above.. size should be 4...,

>
> Where did you get such a silly idea? You will note that your comment
> "//will give 1" is also untrue for the implementation on which I ran the
> above program.


I don't think an idea is silly or stupid, it's either plausible or not
by currently available means but definitely not silly. Infact the
compiler should be able to tell how many bits a bit field represents,
whether it's useful or not shouldn't bother any one but the one who
requires this information.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-04-2006
chinu wrote:
>>>
>>>is it possible to find the size of a structure upto bit size.., where
>>>the structure contains only one element and that too with bit
>>>specifier..eg..
>>>
>>>struct x {
>>> char a : 4;


A cautionary nit-pick: The only portable "base types" for
bit-fields are signed and unsigned `int', plus `_Bool' in C99.
The compiler is allowed to accept other types like `char' if
it wishes, but the next compiler you use is allowed to reject
them.

>>>} y;
>>>

> [... and in a follow-up:]
>
> point comes in different context.. it would be helpful in embedded
> system env.. where i need to know the size of individual bits to work
> on them only.., othervise i need to remember the width of each field..


Why do you "need to remember the width" when the compiler
is already remembering it for you? If you write `y.a = 3' the
compiler will figure out which bits to set and clear, and where
they're located within `y'; you don't need to remember anything
except that `y' contains an `a' of integer type. What else do
you need to remember, and why?

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
padding granularity ranjeet.gupta@gmail.com C Programming 1 06-27-2005 10:33 AM
Notable granularity when using uji FinePix S5100 a Digital Photography 7 04-08-2005 12:54 AM
Notable granularity when taking indoor photos using Fuji FinePix S5100 Bill Digital Photography 8 04-03-2005 06:49 PM
Bandwidth Reservation Granularity Amir Gourgy Cisco 0 09-04-2004 12:34 AM
Netflow granularity Ben Horner Cisco 0 02-16-2004 02:50 PM



Advertisments