ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I see some code about Hostap project,in these codes,I found some structure which have such signs: __attribute__((packed)),I don't know what does this mean. So soon I found more such signs in Linux Kernel Souce code.Who can tell me what does this mean exactly and how I use it?Thanks
Here is how I think it works (please correct me if I'm wrong!)
__attribute__((packed)) ensures that structure fields align on one-byte boundaries. If you want to ensure that your structures have the same size on all processors, the packed attribute is how you tell gcc.
As an example, let's define this structure:
Code:
struct s {
char aChar;
int anInt;
};
A processor that aligns on eight-byte boundaries may compile this so that aChar is in the first byte, followed by seven bytes of unused space, then starting anInt in the ninth byte.
A processor that aligns on four-byte boundaries may compile this so that aChar is in the first byte, followed by three bytes of unused space, then starting anInt in the fifth byte.
To force anInt to begin immediately after aChar, you would define the structure like this:
Code:
struct s {
char aChar;
int anInt __attribute__((packed));
};
To test these ideas out, I ran this code on an old Pentium 166:
Code:
#include <stdio.h>
struct s1 {
char a;
int i;
};
struct s2 {
char a;
int i __attribute__((packed));
};
int main( int argc, char* argv[] ) {
struct s1 s_1;
struct s2 s_2;
printf( "sizeof s1 is %d\n" , sizeof(s_1) );
printf( "sizeof s2 is %d\n" , sizeof(s_2) );
return( 0 );
}
And got these results:
Code:
eric.r.turner@turing:~/lab/packed$ ./foo
sizeof s1 is 8
sizeof s2 is 5
Looks like this processor aligns on four-byte boundaries.
Last edited by eric.r.turner; 06-14-2004 at 10:14 PM.
void *a => 32-bit processor means this is 32 bits wide (or 4 bytes)
char b[2] => a char is typically 1 byte. You have two of them. So b occupies 2 bytes
int i => most 32-bit machines also default to 32 bits for plain integers (again, 4 bytes)
For your non-packed structure, you processor is aligning to 4-byte boundaries. Thus, a fills one full 4-byte block, b fills two bytes of the next block (leaving 2 other bytes unused), and i occupies the next full 4-byte block. So, in that case:
struct s2 {
char a;
int i __attribute__ ((packed));
char b;
int i1;
};
here sizeof(s2) = 12.
But actually size should be 10 ( 1 byte+4 bytes + 1 byte + 4 bytes ),how ?
how it would be ( 1 byte+4 bytes + 1 byte + 2 bytes + 4 bytes ) =
sizeof(a)+ sizeof(i) +sizeof(b)+ 2 zeros + sizeof(i1))
here what is this another 2 bytes ( 2 zeros ) ? Can anyone ,please explain ?
struct s2 {
char a;
int i __attribute__ ((packed));
char b;
int i1;
};
here sizeof(s2) = 12.
But actually size should be 10 ( 1 byte+4 bytes + 1 byte + 4 bytes ),how ?
how it would be ( 1 byte+4 bytes + 1 byte + 2 bytes + 4 bytes ) =
sizeof(a)+ sizeof(i) +sizeof(b)+ 2 zeros + sizeof(i1))
here what is this another 2 bytes ( 2 zeros ) ? Can anyone ,please explain ?
Thanks in advance .
regards,
Rajesh.
The processor is adding an extra two bytes after b so that the next integer starts on an 8 byte boundary. If you want the integer to start immediately after the char, you have to tell the processor to pack it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.