LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-14-2004, 08:46 PM   #1
frankli
LQ Newbie
 
Registered: Dec 2003
Posts: 25

Rep: Reputation: 15
What's this: __attribute__((packed))


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
 
Old 06-14-2004, 09:59 PM   #2
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 208

Rep: Reputation: 31
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.
 
1 members found this post helpful.
Old 06-14-2004, 11:58 PM   #3
frankli
LQ Newbie
 
Registered: Dec 2003
Posts: 25

Original Poster
Rep: Reputation: 15
thanks a lot!
 
Old 06-15-2004, 12:54 AM   #4
frankli
LQ Newbie
 
Registered: Dec 2003
Posts: 25

Original Poster
Rep: Reputation: 15
But when I wirte the progarm like this:
struct s1 {
void *a;
char b[2];
int i;
};

struct s2 {
void *a
char b[2];
int i ;
}__attribute__((packed));

and got the result like this:
sizeof s1 is 12
sizeof s2 is 10
WHY? I think sizeof s2 should be 11!
 
Old 06-15-2004, 01:56 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
Why would it be 11?

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)

So sizeof(a) + sizeof(b) + sizeof(i) = 4 + 2 + 4 = 10

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:

sizeof(a) + sizeof(b) + sizeof(wasted space) + sizeof(i) = 4 + 2 + 2 + 4 = 12
 
Old 06-15-2004, 03:20 AM   #6
frankli
LQ Newbie
 
Registered: Dec 2003
Posts: 25

Original Poster
Rep: Reputation: 15
thanks,I made a mistake.
 
Old 03-16-2011, 02:50 AM   #7
vijayviji
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Rep: Reputation: 0
struct s2 {
char a;
int i __attribute__ ((packed));
char b;
int i1;
};

(on 32-bit machine)
Its showing the size of this structure is 12. Can you plz tell me why ?
 
Old 03-16-2011, 03:00 AM   #8
vijayviji
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Rep: Reputation: 0
Yup.. I found it..

Its allocating as follows,

1 byte + 4 bytes + 1 byte + 2 bytes + 4 bytes
char a, int i, char b, zeroes, int i1
 
Old 03-16-2011, 07:16 AM   #9
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 208

Rep: Reputation: 31
Quote:
Originally Posted by vijayviji View Post
Yup.. I found it..
LOL, this thread is nearly seven years old!
 
Old 03-16-2011, 03:52 PM   #10
james strouth
LQ Newbie
 
Registered: Mar 2011
Distribution: Fedora
Posts: 1

Rep: Reputation: 0
I found this thread helpful. Thanks for the __attribute__ ((packed)) explaination.
 
Old 09-11-2012, 01:52 AM   #11
rajesh180186
LQ Newbie
 
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
hi ,

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.
 
Old 09-11-2012, 02:47 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,456

Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
You might want to use two packed's:

Code:
struct s2 {
    char a;
    int i __attribute__ ((packed));
    char b;
    int i1 __attribute__ ((packed));
};
But please note that this packed stuff:

- is a non-standard extension
- slows down the processing
- should be avoided
 
Old 09-11-2012, 08:32 AM   #13
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 208

Rep: Reputation: 31
Quote:
Originally Posted by rajesh180186 View Post
hi ,

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.
 
Old 09-11-2012, 09:03 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,456

Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
it's four, actually, not eight
 
Old 09-11-2012, 09:37 AM   #15
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Why not use the pack pragma to set the alignment around the structure? IE:

Code:
#pragma pack(1)

struct s2 {
    char a;
    int i;
    char b;
    int i1;
};

#pragma pack()
sizeof(struct s2): 10

Whereas:

Code:
struct s2 {
    char a;
    int i;
    char b;
    int i1;
};
sizeof(struct s2): 16
 
  


Reply

Tags
gcc


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting vop-packed DivX to MPEG CanadianPenguin Linux - Software 5 09-07-2010 04:08 PM
what do all those __attribute__(()) mean? extremeware Programming 3 05-17-2005 03:09 PM
Are ppl buying computers packed with linux. Do u have one? RHLinuxGUY Linux - General 6 09-16-2004 10:06 AM
Grub packed up and left J.Q. Monkey Linux - Newbie 3 01-08-2004 03:21 PM
Diald and mysterious packed daja58 Linux - Networking 0 12-02-2000 05:20 PM


All times are GMT -5. The time now is 07:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration