linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* structure size
@ 2003-03-05  7:02 Mohammed Khalid Ansari
  2003-03-05  8:27 ` Helmut Djurkin
  0 siblings, 1 reply; 3+ messages in thread
From: Mohammed Khalid Ansari @ 2003-03-05  7:02 UTC (permalink / raw)
  To: linux c programming mailing list


Hi,

why the sizeof sturcture is alway greater than the collective sizes of all 
the elements in it.

eg
struct node {
	int	a;
	char	c;
	int	b;
};

int main()
{
	printf ("%d\n", sizeof (struct node));
	return 0;
}


it prints 12 in my case.

where is the catch.

-- 

**************************************************************************

Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
Assistant Manager II                          (off) : 0091-022-2024641
National Centre for Software Technology   Fax       : 0091-022-2049573 
8th flr,Air India Build. Nariman Point,   E-Mail    : khalid@ncst.ernet.in 	
Mumbai 400021.

Homepage : http://soochak.ncst.ernet.in/~khalid			  	  

**************************************************************************


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: structure size
  2003-03-05  7:02 Mohammed Khalid Ansari
@ 2003-03-05  8:27 ` Helmut Djurkin
  0 siblings, 0 replies; 3+ messages in thread
From: Helmut Djurkin @ 2003-03-05  8:27 UTC (permalink / raw)
  To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list

hi,
it's the alignment.

with gcc and this:
struct node {
	/* member here */
} __attribute__((packed));

you will get the your "collective sizes".


Mohammed Khalid Ansari wrote:
> Hi,
> 
> why the sizeof sturcture is alway greater than the collective sizes of all 
> the elements in it.
> 
> eg
> struct node {
> 	int	a;
> 	char	c;
> 	int	b;
> };
> 
> int main()
> {
> 	printf ("%d\n", sizeof (struct node));
> 	return 0;
> }
> 
> 
> it prints 12 in my case.
> 
> where is the catch.
> 




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: structure size
       [not found] <65256CE0.00269E0A.00@sandesh.hss.hns.com>
@ 2003-03-06  4:03 ` Mohammed Khalid Ansari
  0 siblings, 0 replies; 3+ messages in thread
From: Mohammed Khalid Ansari @ 2003-03-06  4:03 UTC (permalink / raw)
  To: hbajaj; +Cc: linux c programming mailing list


On Wed, 5 Mar 2003 hbajaj@hss.hns.com wrote:

> 
> 
> the structure size is always multiple of the size of element with max size in
> that structure.
> Suppose
> struct {
> int a;
> char b;
> }
> then size of struct will be multiple of the size of int i.e 4 on solaris.So size
> will be 8 bytes as sizeof(a)+sizeof(b) = 5 so to make it multiple of 4 it pads
> it with 3 bytes.

Why it is so? any specific reason!

> if we have
> struct {
> short int a;
> char b;
> }
> then size of struct will be multiple of the size of int i.e 2 on solaris.
> 
> Rgds
> Harvinder Bajaj
> 
> 
> 
> 
> Mohammed Khalid Ansari <khalid@ncst.ernet.in> on 05-03-2003 12:32:01 PM
> 
> To:   linux c programming mailing list <linux-c-programming@vger.kernel.org>
> cc:    (bcc: Harvinder Bajaj/HSS)
> 
> Subject:  structure size
> 
> 
> 
> 
> 
> Hi,
> 
> why the sizeof sturcture is alway greater than the collective sizes of all
> the elements in it.
> 
> eg
> struct node {
>      int  a;
>      char c;
>      int  b;
> };
> 
> int main()
> {
>      printf ("%d\n", sizeof (struct node));
>      return 0;
> }
> 
> 
> it prints 12 in my case.
> 
> where is the catch.
> 
> --
> 
> **************************************************************************
> 
> Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
> Assistant Manager II                          (off) : 0091-022-2024641
> National Centre for Software Technology   Fax       : 0091-022-2049573
> 8th flr,Air India Build. Nariman Point,   E-Mail    : khalid@ncst.ernet.in
> Mumbai 400021.
> 
> Homepage : http://soochak.ncst.ernet.in/~khalid
> 
> **************************************************************************
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming"
> in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-03-06  4:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <65256CE0.00269E0A.00@sandesh.hss.hns.com>
2003-03-06  4:03 ` structure size Mohammed Khalid Ansari
2003-03-05  7:02 Mohammed Khalid Ansari
2003-03-05  8:27 ` Helmut Djurkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).