granularity of size of operator

C

chinu

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
 
J

jacob navia

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.
 
K

Keith Thompson

chinu said:
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.
 
S

slebetman

jacob said:
chinu a écrit :

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.
 
C

chinu

jacob said:
chinu a écrit :

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..
 
R

Richard

jacob navia said:
chinu a écrit :

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.
 
G

goose

chinu said:
jacob navia wrote:


no, its not about addressing the bits individually.

I'd be interested to know what you plan to do with
the size in bits.
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,
 
M

Martin Ambuhl

chinu said:
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.
 
H

Haroon Shafiq

Martin said:
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.
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.
 
E

Eric Sosman

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.
[... 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?
 
A

Ancient_Hacker

chinu said:
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
}


probably not doable at compile-time. You have a better chance of doing
it at run-time by doing a game of twenty questions, something like:

unsigned long int i; struct y q;

for( i=q.a=1; i == q.a; i <<= 1,q.a <<= 1 ) ;

printf("q.a has %d bits\n", i );


.... then again, there's no guarantee, the compiler might see there's
nothing else in that byte and not bother doing the masking. Or the
signedness of a may break the comparison.

I'e always wanted a listing option where the compiler would explicitly
list out how it packed structs, for those cases where you need a C
struct to exactly match some OS structure, or other language structure.
For example, Fortran array descriptors have a word full of important
flag bits, it would be nice to be able to access them without a lot of
folderol. Or PC parallel port registers, which are a maze of about 28
packed bit fields.
It's usually done by a whole gob of #define FIFO_RESET_SHIFT 13 and
#define FIFO_RESET_MASK 0x7, but a nice packed struct would be awhole
lot cleaner to use.
I know, non-portable.
 
K

Keith Thompson

chinu said:
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

You can always remember it yourself:

#define A_SIZE 4
struct x {
unsigned int a : A_SIZE;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top