Sizeof structure

Go down

Sizeof structure

Post  Admin on Sun Aug 10, 2008 9:02 am

struct a{
int b;
char c;
float d;
};
struct a1{
int b1;
int c1;
float d1;
};
main()
{
printf("%d %d",sizeof(struct a),sizeof(struct a1));
}

Both the sizes come to be equal on gcc compiler.Why is it so?

Admin
Admin

Posts : 39
Join date : 2008-08-03

View user profile http://computerclub09mnnit.forumotion.net

Back to top Go down

Re: Sizeof structure

Post  VAIBHAV SINGHAL on Sun Aug 10, 2008 10:35 am

answer of this problem is ...
THE SIZE OF THE STRUCTURE EQUAL TO THE SIZE OF THE ELEMENT HAVING THE LARGEST SIZE IN THAT STRUCTURE.

VAIBHAV SINGHAL

Posts : 2
Join date : 2008-08-10
Age : 28
Location : tilak 149 allahabad

View user profile

Back to top Go down

Re: Sizeof structure

Post  amazon on Sun Aug 10, 2008 10:57 am

yaar par wo to union mein hota hai!!!!!

amazon

Posts : 13
Join date : 2008-08-10

View user profile

Back to top Go down

Re: Sizeof structure

Post  ankurgutpa on Sun Aug 10, 2008 11:06 am

not really
this concept is known as structure padding
struct s1{
int a
char b
char c
}
size=8
actually pad of size allocated is of maximum identifier. But if another identifier of size shorter than largest, its get fit into that memory space,like in above example "char b" will take 4 bytes but consume only 1 bye and remaining 3 empty.Now if another identifier ("char c") come ,first of all, it'll be checked that whether or not extra memory is available or not,and yes there are empty 3 bytes with "char b" and it will fir into it.If "char c" would have been "int" then it cannt be fit into 3 bytes so it'll take another 4 bytes.It's something like disk blocks but with a constraint that if enough space is left in block so that a file can be stored it is kept there.
try this:
struct s2{
int a;
char b,c,d,e;
}
size =8;
and think on this;
struct s3{
cahr a;
int b;
char c;
}
size=12
//it also depend upon order in which we declare cheers
avatar
ankurgutpa

Posts : 44
Join date : 2008-08-10
Age : 30
Location : Tandon 72

View user profile

Back to top Go down

Re: Sizeof structure

Post  pheonix on Sun Aug 10, 2008 1:50 pm

it shud come 9 ,, No

pheonix

Posts : 1
Join date : 2008-08-10

View user profile

Back to top Go down

Re: Sizeof structure

Post  ankurgutpa on Sun Aug 10, 2008 2:07 pm

no dear,
it should come 12,
check it out carefully
first char a; (1 byte)
int b; (4 byte and all blocks of 4 bytes) (total=8
char c( 4 byte it does not care for previous declaration of any data type but size pad )(total=12)
3 empty bytes of char a cannot be allocated as is declared before the larger data type(int)
...if now also u r not satisfied run it on gcc compiler cheers
avatar
ankurgutpa

Posts : 44
Join date : 2008-08-10
Age : 30
Location : Tandon 72

View user profile

Back to top Go down

Re: Sizeof structure

Post  shivang on Mon Aug 11, 2008 8:06 am

yes ankur is right...alignment and padding is done while storing these structures. consider the two types of example:

Structure member alignment:
Suppose you have the following structure:

struct A1
{
char a[2];
int b;
};

You could think that sizeof(A1) equates to 6, but it doesn’t. It equates to 8. The compiler inserts 2 dummy bytes between members ‘a’ and ‘b’.
The reason is that the compiler will align member variables to a multiple of the pack size or a multiple of the type size, whichever is smallest.
‘b’ is of the integer type, which is 4 bytes wide. ‘b’ will be aligned to the minimum of those 2, which is 4 bytes. It doesn’t matter if ‘a’ is 1, 2, 3 or 4 bytes wide. ‘b’ will always be aligned on the same address.

Structure end padding
Suppose we swap members ‘a’ and ‘b’:
struct A2
{
int b;
char a[2];
};

The compiler places ‘a’ right next to ‘b’ without padding, because the address after ‘b’ is naturally aligned for ‘a’.
Still, sizeof(A2) equates to 8. The reason is that ‘b’ still needs to be aligned on a multiple of 4 bytes if 2 structures of type A2 are placed in an array.
If nothing special was done, member ‘b’ of the second structure would be placed right after member ‘a’ of the first structure. This would not a 4 byte multiple but a 2 byte multiple.
To prevent this, 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.


This can lead to strange situations if you create custom structures without paying attention to these details. This can be illustrated with the following structure if the pack size is left to the default 8 bytes:
struct A3
{
char a;
double b;
char c;
};
‘a’ is 1 byte wide, and aligned to the start of the structure.
‘b’ is 8 bytes wide, but is aligned on the next address multiple of 8 bytes. This means that 7 dummy bytes are inserted between ‘a’ and ‘b’.
‘c’ is 1 byte wide, but the structure A3 itself is padded with 7 extra bytes because its size has to be a multiple of 8.
As a result, the total structure size is a whopping 24 bytes, even though there are only 10 bytes of data inside.
avatar
shivang

Posts : 22
Join date : 2008-08-11
Age : 29
Location : Tondon 174

View user profile

Back to top Go down

Re: Sizeof structure

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum