Re: structre object question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



--- cranium 2003 <cranium.2003@xxxxxxxxx> wrote:

> Hello Mandeep,
> On Mon, 31 Jan 2005 16:29:52 +0530, Mandeep Sandhu
> <Mandeep_Sandhu@xxxxxxxxxxx> wrote:
> > On Mon, 2005-01-31 at 16:20 +0530, cranium 2003
> wrote:
> > > Hello,
> > >   In linux kernel source etheernet header is
> defined as structure as
> > > struct ethhdr
> > > {
> > >         unsigned char   h_dest[ETH_ALEN];      
> /* destination eth addr */
> > >         unsigned char   h_source[ETH_ALEN];    
> /* source ether addr    */
> > >         unsigned short  h_proto;               
> /* packet type ID field */
> > > } __attribute__((packed));
> > > I want to know what __attribute__((packed))
> meance?
> > >        It is not looking as a structure object 
> then what is thati?
> > 
> > Your ethernet header is 14 bytes long so the
> "packed" attribute
> > tells the compiler to to pack the structure to 14
> bytes and not
> > try to byte align it on a word boundary (as it
> would do without
> > specifying this keyword).
>     Why its necessary to tell compiler to consider
> it as 14 bytes?
> becasue  sizeof(struct ethhdr) is 14 bytes so while
> putting ethhdr in
> packet 14 bytes are always put so why question
> arises to have word
> boundry alignment?

Normally the compiler allocates the memory which is a
a multiple of word size. So in 32 bit machines,
compilers allocate, 4,8,12,16,20 .... 32 and so on
bytes of memory which is sufficent to contain the
value.

So in case of ethernet header if we do not mention the
packed attribute, on 32 bite machine compiler will
allocate 16 bytes for this structure, but if we want
the exact number of bytes as dictated by us, we use
the packed attribute. In this case compiler will only
allocate 14 bytes to structure which is the actual
size of our structure without padding extra bytes.

cheers !!
--gd




=====
-------------------------------------------
Subscribe LKG_INDIA Group :
Its Linux Kernel oriented group, started by bunch of guys from India.
This group discusses the technical details of Linux Kernel, helping the members to share and enhance their knowledge on Linux Kernel and general OS concepts.
Be a member of it if you are Linux Kernel Geek.
For joining this group, mail to gauravd_chd@xxxxxxxxx
Visit the LKG_INDIA home page: http://groups.yahoo.com/group/lkg_india/
-------------------------------------------


		
__________________________________ 
Do you Yahoo!? 
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com 

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux