473,473 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

structure

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
10 2282
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


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


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


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


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
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
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
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 (40°39.22'N, 111°50.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
4
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
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...
0
marktang
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,...
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
jinu1996
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...
1
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
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,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.