Results 1 to 7 of 7

Thread: What is Structure Padding in C ?

  1. #1
    Join Date
    Jul 2009
    Posts
    104

    What is Structure Padding in C ?

    Hi,

    Can any one explain me about structure padding and the memory pools in detail. And What is the use of padding?

  2. #2
    Join Date
    Mar 2008
    Posts
    227

    Re: What is Structure Padding in C ?

    Most processors require specific memory alignment on variables certain types. Normally the minimum alignment is the size of the basic type in question, fo instance this is common

    Char variables can be byte aligned and appear at any byte boundary

    Short (2 byte) variables must be 2 byte aligned, they can appear at any even byte boundary. This means that 0x10004567 is not a valid location for a short variable but 0x10004566 is.

    Long (4 byte) variables must be 4 byte aligned, they can only appear at byte boundarys that are a multiple of 4 bytes. This means that 0x10004566 is not a valid location for a long variable but 0x10004568 is.

    Structure padding occurs because the members of the structure must appear at the correect byte boundary, to achieve this the compiler puts in padding bytes (or bits if bit fields are in use) so that the structure members appear in the correct location. Additionally the size of the structure must be such that in an array of the structures all the structures are correctly aligned in memory so there may be padding bytes at the end of the structure too

    Code:
    struct example { 
    char c1; 
    short s1; 
    char c2; 
    long l1; 
    char c3; 
    }
    In this structure, assuming the alignment scheme I have previously stated then

    c1 can appear at any byte boundary, however s1 must appear at a 2 byte boundary so there is a padding byte between c1 and s1.

    c2 can then appear in the available memory location, however l1 must be at a 4 byte boundary so there are 3 padding bytes between c2 and l1

    c3 then appear in the available memory location, however because the structure contains a long member the structure must be 4 byte aligned and must be a multiple of 4 bytes in size. Therefore there are 3 padding bytes at the end of the structure. it would appear in memory in this order
    Code:
    c1 
    padding byte 
    s1 byte 1 
    s1 byte 2 
    c2 
    padding byte 
    padding byte 
    padding byte 
    l1 byte 1 
    l1 byte 2 
    l1 byte 3 
    l1 byte 4 
    c3 
    padding byte 
    padding byte 
    padding byte
    The structure would be 16 bytes long.

    re-written like this
    Code:
    struct example { 
    long l1; 
    short s1; 
    char c1; 
    char c2; 
    char c3; 
    }
    Then l1 appears at the correct byte alignment, s1 will be correctly aligned so no need for padding between l1 and s1. c1, c2, c3 can appear at any location. The structure must be a multiple of 4 bytes in size since it contains a long so 3 padding bytes appear after c3

    It appears in memory in the order

    Code:
    l1 byte 1 
    l1 byte 2 
    l1 byte 3 
    l1 byte 4 
    s1 byte 1 
    s1 byte 2 
    c1 
    c2 
    c3 
    padding byte 
    padding byte 
    padding byte
    and is only 12 bytes long.

    I should point out that structure packing is platform and compiler (and in some cases compiler switch) dependent.

    Memory Pools are just a section of memory reserved for allocating temporarily to other parts of the application

    A memory leak occurs when you allocate some memory from the heap(or a pool) and then delete all references to that memory without returning it to the pool it was allocated from.



    Source : Wiki.answers

  3. #3
    Join Date
    Mar 2008
    Posts
    349

    Re: What is Structure Padding in C ?

    The Structure Padding is done by the compiler for performance. Most hardware architectures shortcut directions if they are aligned properly, otherwise there will be no performance penalty. To enhance the performance of the compiler pads structures. Structure members of the filling will be done by compiler and structure as a whole also. Compiler pads structure as a whole because it allows each member of the structure aligned in the array of structures.

  4. #4
    Join Date
    May 2008
    Posts
    353

    Re: What is Structure Padding in C ?

    Structure Padding is for the quick access of data from memory. Structure members is aligned based on the size of memory pointer, which normally is 32 bits in Win32 systems. So a defined character variable can take 4 bytes. Therefore structure size is not really the sum of the size of individual members.

  5. #5
    Join Date
    Jun 2011
    Posts
    1

    Re: What is Structure Padding in C ?

    Hello DIID,

    Well explained on Structure Padding.

  6. #6
    Join Date
    Mar 2008
    Posts
    227

    Re: What is Structure Padding in C ?

    hey thanks nirmalah. I just wish this all information are useful to you. Let me know if you want any further information on this. I really like to help. Once more thanks to liking my information.

  7. #7
    Join Date
    May 2012
    Location
    INDIA
    Posts
    1

    Re: What is Structure Padding in C ?

    Hey here During Padding Few bytes of memory is Wasting!, This is not needed for limited memory Application... How That Wastage is overcome

Similar Threads

  1. customize Window Border padding in Windows 8
    By Fella in forum Customize Desktop
    Replies: 4
    Last Post: 21-06-2013, 10:59 AM
  2. Css padding in html
    By klite in forum Software Development
    Replies: 7
    Last Post: 31-12-2009, 11:00 AM
  3. memory alignment and structure padding
    By lolguy in forum Software Development
    Replies: 2
    Last Post: 10-10-2009, 10:25 AM
  4. Html need to know about text wrap padding around an image
    By ComPaCt in forum Software Development
    Replies: 2
    Last Post: 09-07-2009, 08:30 PM
  5. Problem with width for nested div padding
    By Jaisudha in forum Software Development
    Replies: 3
    Last Post: 22-06-2009, 12:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,614,682.61202 seconds with 17 queries