449,309 Members | 2,035 Online
+ 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
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 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