The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Padding with zeros. sbasetty Shell Programming and Scripting 10 05-18-2009 07:09 PM
Padding in Unix rudoraj Shell Programming and Scripting 3 02-21-2008 12:06 AM
AWK and padding values kshelluser UNIX for Dummies Questions & Answers 3 11-29-2006 05:14 PM
Padding vijaygopalsk UNIX for Dummies Questions & Answers 2 06-27-2003 10:51 AM
Padding issues informshilpa UNIX for Advanced & Expert Users 2 03-01-2002 01:51 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-13-2007
naan naan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 31
Byte Padding

Hi,

Can someone explain what is byte padding?

For ex:
struct emp{
char s;
int b;
char s1;
int b1;
long b3;
char s3;
}

What will be the size of this structure?

Thanks
  #2 (permalink)  
Old 02-13-2007
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Check for the sizeof operator like:
Code:
#include <stdio.h>

typedef struct{
char s;
int b;
char s1;
int b1;
long b3;
char s3;
}emp;

void main() {
emp tst1;
printf ("Size: %d\n", sizeof(tst1));
}
Output at my machine:
Code:
Size: 24
  #3 (permalink)  
Old 02-13-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Padding is implementation dependent. Basically it means the the objects inside the struct have their beginning addresses moved (padded with bytes) so the start of each object can be accessed by the hardware efficiently.

Misalignment of objects can result in a SIGBUS core dump, or very much slower access to the object. Some compilers have a pragma to turn off or alter padding. Others provide warnings about padding.
  #4 (permalink)  
Old 02-19-2007
rv1125 rv1125 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 3
for gcc try to define your structure like this:

struct gtpp_hdr6 {
unsigned char flag; // 1 byte
unsigned char message_type; // 1 byte
unsigned short length; // 2 bytes
unsigned short sequence; // 2 bytes
} __attribute__((__packed__));

and check again.
This attribute shows compiler that this structure used for data packets and compiler will not do padding
  #5 (permalink)  
Old 02-20-2007
girishsjce girishsjce is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 1
Arrow Padding Issues.

Its actually low level issue. Padding stores all the data element started from even addresses. In this mechanism char also take 2 bytes ( one Byte stored in current even address, next data element can store in next even address, in b/w odd address is wasted). But it improves data reading efficiency.
  #6 (permalink)  
Old 02-21-2007
moshe fried moshe fried is offline
Registered User
  
 

Join Date: Feb 2007
Location: Israel
Posts: 24
A program that shows padding issues and offsets

Hi,
When working in many client/server situations that data being passed is passed in data structures. It's important to know the padding arrangements between two architects, so here is an example program that shows how to see the offsets.
#include <stdio.h>
int main (void)
{
struct {
char a;
int i;
double d;
} st;

printf ("st size: %d\noffset a: %d\noffset i: %d\noffset d:%d\n",
sizeof (st),
((char *) (&st.a)) - ((char *)&st),
((char *) (&st.i)) - ((char *)&st),
((char *) (&st.d)) - ((char *)&st));

}
  #7 (permalink)  
Old 02-22-2007
Hitori's Avatar
Hitori Hitori is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2006
Posts: 360
C compilers on different systems lay out structures differently.
In some cases there can even be layout differences between different
C compilers on the same system. Compilers add gaps between fields,
and these gaps have different sizes and are at different locations.
You can normally assume that there are no gaps between fields of type char
or array of char. However, you can not make any assumptions about
gaps between fields of any larger type. You also can not make any
assumptions about the layout of bitfield types.

(From autobook 1.5)
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0