LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-17-2012, 10:18 AM   #1
rpittala
Member
 
Registered: Jan 2012
Location: PUNE
Distribution: SunOS sun4v sparc sun4v Solaris
Posts: 102
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 ?
#include<stdio.h>
int main()
{
struct name{
int a;
char b;
short c;
char d;
short e
int e;
}p;
printf("%d \n",sizeof(p));
return 0;
}
 
Old 04-17-2012, 12:18 PM   #2
manu-tm
Member
 
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, 12:23 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

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

Anyway, about padding:
http://vcfaq.mvps.org/lang/11.htm

In particular:
Quote:
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 12:36 PM.
 
1 members found this post helpful.
Old 04-17-2012, 12:31 PM   #4
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 509

Rep: Reputation: 203Reputation: 203Reputation: 203
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 http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html.
 
1 members found this post helpful.
  


Reply



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 07:33 AM
creating logical volume of exact specified size devkpict Linux - Software 1 07-16-2009 03:59 AM
deleting file by exact size drpaudel Linux - Newbie 1 04-02-2008 03:56 AM
how to calculate kernel structure size? cranium2004 Programming 1 04-15-2005 04:09 AM
Can't recognize my exact harddisk size ! ericmacau Linux - Hardware 2 09-23-2003 05:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:53 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration