Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-17-2012, 11:18 AM   #1
Registered: Jan 2012
Location: PUNE
Distribution: SunOS sun4v sparc sun4v Solaris
Posts: 82
Blog Entries: 1

Rep: Reputation: Disabled
Question how to identify the exact padding size of a structure considering all situations ?

The below code is for your reference :

-->In the below code the size of structure is,I`m expecting it as 20bytes but it is 16 bytes.

-->How the word boundary is aligned for it ?

-->what is the secret behind padding ?

-->how to identify the exact padding size of a structure considering all situations ?
int main()
struct name{
int a;
char b;
short c;
char d;
short e
int e;
printf("%d \n",sizeof(p));
return 0;
Old 04-17-2012, 01:18 PM   #2
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
I suppose it depends on your machine architecture and on your compiler.
Old 04-17-2012, 01:23 PM   #3
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Where do you get 20? I get 14, 4 per int, 2 per short, 1 per char.

Anyway, about padding:

In particular:
the compiler pads the end of the structure with dummy bytes until the structure size is a multiple of the largest alignment in the structure. That way the alignment for all consecutive structures is valid.
Your largest type is 4 bytes, so the struct is padded up to the nearest multiple of 4, which is 16. This behavior can be modified with the pragma described in the above link.

Each compiler will likely be different, but it sounds like yours is doing something similar to this.

Last edited by suicidaleggroll; 04-17-2012 at 01:36 PM.
1 members found this post helpful.
Old 04-17-2012, 01:31 PM   #4
Registered: Jan 2012
Location: South Africa
Posts: 500

Rep: Reputation: 189Reputation: 189
This may depend on the word size of your machine, your compiler's default behaviour, and any options you may have passed to the compiler to change the default behaviour.

Assuming you are using gcc, it appears that the default is to "pack all structure members together without holes", but that you can change this with the "-fpack-struct[=n]" option. See
1 members found this post helpful.


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
exact size of UDF image for 50G bluray ? fishjohn Linux - Hardware 3 08-05-2010 08:33 AM
creating logical volume of exact specified size devkpict Linux - Software 1 07-16-2009 04:59 AM
deleting file by exact size drpaudel Linux - Newbie 1 04-02-2008 04:56 AM
how to calculate kernel structure size? cranium2004 Programming 1 04-15-2005 05:09 AM
Can't recognize my exact harddisk size ! ericmacau Linux - Hardware 2 09-23-2003 06:07 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:48 AM.

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