LinuxQuestions.org
Visit Jeremy's Blog.
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, 11:18 AM   #1
rpittala
Member
 
Registered: Jan 2012
Location: PUNE
Distribution: SunOS sun4v sparc sun4v Solaris
Posts: 56
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, 01: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, 01:23 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,260

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
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 01:36 PM.
 
1 members found this post helpful.
Old 04-17-2012, 01:31 PM   #4
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 481

Rep: Reputation: 179Reputation: 179
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


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


All times are GMT -5. The time now is 06:21 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration