473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

granularity of size of operator

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

Aug 4 '06 #1
11 2793
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.

Aug 4 '06 #2
"chinu" <ch**********@gmail.comwrites:
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) 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.
Aug 4 '06 #3
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.

Aug 4 '06 #4

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.
Aug 4 '06 #5
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,

Aug 4 '06 #6
jacob navia <ja***@jacob.remcomp.frwrites:
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.
Aug 4 '06 #7
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.
Aug 4 '06 #8

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.

Aug 4 '06 #9
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
es*****@acm-dot-org.invalid
Aug 4 '06 #10

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
}

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.

Aug 4 '06 #11
"chinu" <ch**********@gmail.comwrites:
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;
}

--
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.
Aug 4 '06 #12

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

Similar topics

12
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
2
by: Rasmus Aveskogh | last post by:
Hi, Since I went from Oracle to PostgreSQL I've been missing the "invisable" column 'rownum'. I often used it to lower the granularity of my data. For example, say I have a large table...
17
by: Arnold | last post by:
Is using fseek and ftell a reliable method of getting the file size on a binary file? I thought I remember reading somewhere it wasn't... If not what would be the "right" and portable method to...
1
by: ranjeet.gupta | last post by:
Dear All What is padding granularity ? Please give me the basic concept of this. Regards Ranjeet
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
30
by: Angel Tsankov | last post by:
Hello! Does the C++ standard define what happens when the size argument of void* operator new(size_t size) cannot represent the total number of bytes to be allocated? For example: struct S {...
9
by: amateur073532 | last post by:
Hi, I have a problem with my new assignment.It requires me to make a book class(contains title,author,publisher,ISBN)and a library class that holds the books.I'm asked to use operator overloading...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.