By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,309 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,309 IT Pros & Developers. It's quick & easy.

structure

P: n/a
Dear All !!

Before i qoute my querry, I will like to qoute my analysis and my
Knowledge

Struct a {
int raw;
char data;
};

the size of above struct is 8 byte ... correct (I am talking of
32 bit procesor)

Struct a {
int raw;
char data_1;
char data_2;
};

again the size of above structure is 8 bytes.

Means the structure of padding is going on... right...

Now suppose I have to specefically allocate the 4 bits,
then What i will do is

strcut a {

cahr a:4;
}

now ideally speaking i should get the size of the above struct as
4 bits, But infact i will get the 1 byte, Again i run in the situation
that i dont want to allocate the extra 4 bits (which by default
structure is doing padding),

now my question comes.

First
how I am going to allocate the 4 bits only,

second (not directly realated to the above)
how i will assure that structure is alligned to the machine lenght ?
what i have to do in the structure that make me sure that it is
alligned to the machine word lenght.

I supposse that Strcuture padding is done to get the exectution of
Arthematic execution fast.... m i correct as it is alligned
to the machine length.

Please let me know about the above querry

Thanks In Advance
Ranjeet

Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
1. by saying,

char a:4;

you are not just allocating 4 bits, instead, you are requesting
for a bit-wise allocation. Suppose, you know that you have three
attributes, with known ranges, you could say,

struct bitwise_alloc
{
char a:4; (range 0 to 15)
char b:2; (range 0 to 3)
char c:1; (range 0 to 1)
char d:1;
};

The size would still be 1 byte (if padding is not there). No allocation
below 1 byte is possible (for an object/variable).

2. you could do this using the compiler directive (#pragma pack),
but then it might not be a generic implementation.

Nov 14 '05 #2

P: n/a


ra***********@gmail.com wrote:
Dear All !!

Before i qoute my querry, I will like to qoute my analysis and my
Knowledge

Struct a {
int raw;
char data;
};

the size of above struct is 8 byte ... correct (I am talking of
32 bit procesor)

Struct a {
int raw;
char data_1;
char data_2;
};

again the size of above structure is 8 bytes.

Means the structure of padding is going on... right...

Now suppose I have to specefically allocate the 4 bits,
then What i will do is

strcut a {

cahr a:4;
} bitfield are appropriate for *int*s

now ideally speaking i should get the size of the above struct as
4 bits, But infact i will get the 1 byte, Again i run in the situation
that i dont want to allocate the extra 4 bits (which by default
structure is doing padding),
You can get size of a structure (infact any data type or variable)
atleast in multiples of byte.


now my question comes.

First
how I am going to allocate the 4 bits only, I think, allocating a memory of 4bits is not possible. However you can
specify __attribute__((__packed__)) along with structure definition to
pack the allocation to byte aligned.

second (not directly realated to the above)
how i will assure that structure is alligned to the machine lenght ?
what i have to do in the structure that make me sure that it is
alligned to the machine word lenght.
__attribute__ ((__aligned__)) might help you.

--
Mohan

I supposse that Strcuture padding is done to get the exectution of
Arthematic execution fast.... m i correct as it is alligned
to the machine length.

Please let me know about the above querry

Thanks In Advance
Ranjeet


Nov 14 '05 #3

P: n/a


Mukul Pandey wrote:
1. by saying,

char a:4;

you are not just allocating 4 bits, instead, you are requesting
for a bit-wise allocation. Suppose, you know that you have three
attributes, with known ranges, you could say,

struct bitwise_alloc
{
char a:4; (range 0 to 15)
char b:2; (range 0 to 3)
char c:1; (range 0 to 1)
char d:1;
};

The size would still be 1 byte (if padding is not there). No allocation
below 1 byte is possible (for an object/variable).
yes you are correct what you are saying.

2. you could do this using the compiler directive (#pragma pack),
but then it might not be a generic implementation.


It will be help me if you give me some pointer on this
(Any web link / Pseduo code)

Nov 14 '05 #4

P: n/a


Mohan wrote:
ra***********@gmail.com wrote:
Dear All !!

Before i qoute my querry, I will like to qoute my analysis and my
Knowledge

Struct a {
int raw;
char data;
};

the size of above struct is 8 byte ... correct (I am talking of
32 bit procesor)

Struct a {
int raw;
char data_1;
char data_2;
};

again the size of above structure is 8 bytes.

Means the structure of padding is going on... right...

Now suppose I have to specefically allocate the 4 bits,
then What i will do is

strcut a {

cahr a:4;
} bitfield are appropriate for *int*s

now ideally speaking i should get the size of the above struct as
4 bits, But infact i will get the 1 byte, Again i run in the situation
that i dont want to allocate the extra 4 bits (which by default
structure is doing padding),


You can get size of a structure (infact any data type or variable)
atleast in multiples of byte.


correct !!

now my question comes.

First
how I am going to allocate the 4 bits only, I think, allocating a memory of 4bits is not possible. However you can
specify __attribute__((__packed__)) along with structure definition to
pack the allocation to byte aligned.


any psedo code or link will be evry helpful to me.

second (not directly realated to the above)
how i will assure that structure is alligned to the machine lenght ?
what i have to do in the structure that make me sure that it is
alligned to the machine word lenght.
__attribute__ ((__aligned__)) might help you.


for this also any psedo code or link will be evry helpful to me.

Thanks in advance
--
Mohan

I supposse that Strcuture padding is done to get the exectution of
Arthematic execution fast.... m i correct as it is alligned
to the machine length.

Please let me know about the above querry

Thanks In Advance
Ranjeet


Nov 14 '05 #5

P: n/a
On Mon, 30 May 2005 03:40:55 -0700, ranjeet.gupta wrote:
Dear All !!

Before i qoute my querry, I will like to qoute my analysis and my
Knowledge

Struct a {
int raw;
char data;
};

the size of above struct is 8 byte ... correct (I am talking of
32 bit procesor)
It may or may not be 8 bytes, even on a 32 bit processor, although
it often is. An implementyation is allowed to make various choices
over object size and alignment requirements.
Struct a {
int raw;
char data_1;
char data_2;
};

again the size of above structure is 8 bytes.
Maybe, maybe not.
Means the structure of padding is going on... right...
That is typical.
Now suppose I have to specefically allocate the 4 bits,
then What i will do is

strcut a {

cahr a:4;
}

now ideally speaking i should get the size of the above struct as
4 bits, But infact i will get the 1 byte,
Often you'll get the word size, perhaps 4 bytes on a 32bit architecture.
Again i run in the situation
that i dont want to allocate the extra 4 bits (which by default
structure is doing padding),
It isn't padding in the same sense, there is an allocation unit that
that compiler will use for bit-fields.
now my question comes.

First
how I am going to allocate the 4 bits only,
You can't do that directly using C's type mechanism, the size of
any non-bitfield object in C must be an integral number of bytes. That
includes any structure type such as struct a, even if it only contains
bit-field members.

The question is fairly meaningless for one bit-field in isolation, the
issue only really arises if you want to place several small fields
together. If you define several adjacent bit-fields in the structure the
compiler will makereasonable efforts to pack them, but the structure as a
while will still be an integral number of bytes (or perhaps even words) in
size. If you want an array of bit-fields C doesn't support this directly
but you can use C's boolean and shift operators to simulate this over an
array of unsigned integer type.
second (not directly realated to the above) how i will assure that
structure is alligned to the machine lenght ? what i have to do in the
structure that make me sure that it is alligned to the machine word
lenght.
You could allocate the structure using malloc() which on success
returns a pointer to memory guaranteed to be appropriately aligned for any
object type in C.

I supposse that Strcuture padding is done to get the exectution of
Arthematic execution fast.... m i correct as it is alligned to the
machine length.


The defined structure is at least aligned so that it can be accessed
correctly. It may or may not be aligned more strictly than that e.g. to
some "machine length".

Lawrence
Nov 14 '05 #6

P: n/a


ra***********@gmail.com wrote:
Dear All !!

Before i qoute my querry, I will like to qoute my analysis and my
Knowledge

Struct a {
int raw;
char data;
};

the size of above struct is 8 byte ... correct (I am talking of
32 bit procesor)

Struct a {
int raw;
char data_1;
char data_2;
};

again the size of above structure is 8 bytes.

Means the structure of padding is going on... right...

Now suppose I have to specefically allocate the 4 bits,
then What i will do is

strcut a {

cahr a:4;
} bitfield are appropriate for *int*s

now ideally speaking i should get the size of the above struct as
4 bits, But infact i will get the 1 byte, Again i run in the situation
that i dont want to allocate the extra 4 bits (which by default
structure is doing padding),
You can get size of a structure (infact any data type or variable)
atleast in multiples of byte.


now my question comes.

First
how I am going to allocate the 4 bits only, I think, allocating a memory of 4bits is not possible. However you can
specify __attribute__((__packed__)) along with structure definition to
pack the allocation to byte aligned.

second (not directly realated to the above)
how i will assure that structure is alligned to the machine lenght ?
what i have to do in the structure that make me sure that it is
alligned to the machine word lenght.
__attribute__ ((__aligned__)) might help you.

--
Mohan

I supposse that Strcuture padding is done to get the exectution of
Arthematic execution fast.... m i correct as it is alligned
to the machine length.

Please let me know about the above querry

Thanks In Advance
Ranjeet


Nov 14 '05 #7

P: n/a
ra***********@gmail.com wrote:
Struct a {
int raw;
char data;
};

the size of above struct is 8 byte ... correct (I am talking of
32 bit procesor)
Incorrect - or more precisely, you do not know this. There may or may
not be padding; the size of int may or may not correspond to any of the
measurements that may lead one to speak of a "32 bit processor"; char
may be any number of bits.
For example, some embedded devices could reasonably be called 32-bits
processors, but on common C implementations for them, both char _and_
int are 32 bits, and the above struct would probably be 2 bytes large,
of 32 bits each.

BTW, C is case-sensitive: that keyword is spelled struct, not Struct.
Struct a {
int raw;
char data_1;
char data_2;
};

again the size of above structure is 8 bytes.
See above: possibly, but you do not know this.
Means the structure of padding is going on... right...
_If_ your assumptions about their sizes are correct, _then_ there is
padding in those structs, yes.
Now suppose I have to specefically allocate the 4 bits,
then What i will do is

strcut a {

cahr a:4;
}

now ideally speaking i should get the size of the above struct as
4 bits,
You can never get any stand-alone C object of 4 bits. All objects are
made of a whole number of bytes, and all bytes are at least 8 bits
large, possibly more.
But infact i will get the 1 byte, Again i run in the situation
that i dont want to allocate the extra 4 bits (which by default
structure is doing padding),
No, it isn't; you've allocated a struct with a single char, and defined
a bit field of four bits in that char.
First
how I am going to allocate the 4 bits only,
Not. It isn't possible in C. It probably isn't possible in any language,
in a meaningful way.
second (not directly realated to the above)
how i will assure that structure is alligned to the machine lenght ?


That is not a meaningful question. First, define "machine length". Then,
explain why you would need to align to it.

All memory you get from malloc(), btw, is aligned to the requirements of
any C type. How this alignment is related to a machine length, well...

Richard
Nov 14 '05 #8

P: n/a
On Mon, 30 May 2005 04:13:29 -0700, Mohan wrote:

....
second (not directly realated to the above)
how i will assure that structure is alligned to the machine lenght ?
what i have to do in the structure that make me sure that it is
alligned to the machine word lenght.


__attribute__ ((__aligned__)) might help you.


However no such construct exists in the C language. Some compilers might
support it as an extension but it is best to avoid compiler specific
constructs if you can.

Lawrence
Nov 14 '05 #9

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>
Mukul Pandey <mu**********@gmail.com> quoted no context, and there
was no previous article on my news server, so I have no idea what
might have been in article
<11**********************@o13g2000cwo.googlegroups .com>, except that
obviously it must have something to do with structure members:
1. by saying,

char a:4;

you are not just allocating 4 bits, instead, you are requesting
for a bit-wise allocation. Suppose, you know that you have three
attributes, with known ranges, you could say,

struct bitwise_alloc
{
char a:4; (range 0 to 15)
char b:2; (range 0 to 3)
char c:1; (range 0 to 1)
char d:1;
};

The size would still be 1 byte (if padding is not there). No allocation
below 1 byte is possible (for an object/variable).
Portable C code can only use "int", "signed int", and "unsigned
int" as bitfield base types. The above often works anyway though,
and is correct as far as it goes. (Note also that in C a "byte"
is a CHAR_BIT-bits-long entity, even if CHAR_BIT is more than 8.
This potentially differs from the 8-bit "octet" that most people
mean when they say "byte".)

There is a large problem with using plain "char" as the bitfield
type, however: plain char may be, and often is, signed. The
"a" member of the struct above would then have a guaranteed
range of -7 to +7 (and a typical range of -8 to +7), rather than
0 to 15. Instead of using plain (possibly-signed) char, if you
need the range to go from 0 to 15, use "unsigned char". (It
may still not even compile, if the compiler does not support
"char" types for bitfield members.)
2. you could do this using the compiler directive (#pragma pack),
but then it might not be a generic implementation.


Indeed. Not only is "#pragma pack" nonstandard, but even those
compilers that do implement it, tend to implement it differently
-- for instance, GCC prefers the __attribute__ spelling. :-)

It is worth noting, however, that no matter whether you use "char"
bitfields, or "#pragma", or "__attribute__", you have thrown
portability entirely out the window. The only difference is
how far out the window it has gone (and whether it has rolled
across the field and into the stream and run out with the river
to the sea :-) ).

In the C code I have seen, people who do this are usually attempting
to match an internal data structure to some external data format
(a file format, or network-data-stream, or hardware register, or
similar). Usually it *can* be done. Often, however, it is a
mistake even to try. C is not Ada; C has no representation clauses.
Instead of trying to trick the compiler into matching up an
internal representation with an external one, the C programmer is
often better off writing several small functions whose entire
purpose is to *translate* between the internal format and the
external one.

For instance, suppose that, instead of the "bitwise_alloc"
structure above, the goal is to read four values from a stdio
"FILE" object, whose ranges are 0..15, 0..3, 0..1, and 0..1,
and which are stored in an 8-bit octet with the "d" value in
bit zero, the "c" value in bit 7, the "b" value in bits 5
and 6 in backwards bit order (i.e., bit 5 is the high bit and
bit 6 is the low bit), and the "a" value in bits 4, 3, 2, and
1 in forwards order. Suppose also that we have:

struct value {
unsigned char val_a; /* range 0..15 */
unsigned char val_b; /* range 0..3 */
unsigned char val_c; /* range 0..1 */
unsigned char val_d; /* range 0..1 */
};

as the internal representation. Then we just need the following
function to read the external format:

int get_value(FILE *stream, struct value *result) {
int c;

if ((c = getc(stream)) == EOF)
return ERROR;
/*
* Value A is stored in bits 4..1: shift to 3..0 and mask.
*/
result->val_a = (c >> 1) & 0xf; /* bits 4..1 */
/*
* Value B is stored in bits 5 (0x20) and 6 (0x40) but
* in backwards bit order. Move bit 6 to bit 0 (shift 6) and
* bit 5 to bit 1 (shift 4).
*/
result->val_b = ((c & 0x20) >> 4) | ((c & 0x40) >> 6);
/*
* Value C is bit 7 (0x80), and value D is bit 0 (0x1).
*/
result->val_c = (c & 0x80) >> 7;
result->val_d = c & 0x01;
return OK;
}

The function required to write the external format should now be
obvious (and is left as an exercise :-) ).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10

P: n/a
On Mon, 30 May 2005 04:09:42 -0700, Mukul Pandey wrote:
1. by saying,

char a:4;
The valid types C supports for bit-fields are int, signed int, unsigned
int and in C99 _Bool. char isn't one of these, although an implementation
is allowed to support it as an extension.
you are not just allocating 4 bits, instead, you are requesting for a
bit-wise allocation. Suppose, you know that you have three attributes,
with known ranges, you could say,

struct bitwise_alloc
{
char a:4; (range 0 to 15)
char b:2; (range 0 to 3)
char c:1; (range 0 to 1)
char d:1;
};


Plain char can be signed or unsigned so it isn't clear that the ranges
would be as you say.

Lawrence
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.