-
Junior Member
How size of structure is defined
How size of structure is defined. If structure members are one character & one integer then size should be 5 bytes.but it is 8 bytes. Why it is so?
-
Contributing Member
Re: How size of structure is defined
Memory is always allocated with respect to address allignment. I mean for 32 bit architecture the address are always 32bit alligned or 4byte alligned.
So when declare a strcuture with one character and one one int variable first 4bytes will be allocated to make it alligned ie what we call padding.
Try this declare a structure with 4 character variables, and one integer variable. the size of structure will be 8bytes.
-
Junior Member
Re: How size of structure is defined
it'll alwyz show 8bytes..... but it'll use only 5bytes..
-
Contributing Member
Re: How size of structure is defined
Yes even if your structure requires only 5bytes in order to make address alignment its padded with 3 more bytes.
Why Padding?
If the processor is serious about alignment it raises an exception/signal (ex. SIGBUS), otherwise there will be a performance penalty
will be there as misalignment slowdown data access.
So every time 4bytes if memory will be assigned irrespective of the data type.
So even if u declare character in a structure 4bytes will be assigned.
Hope its clear, else will answer in more detail
-
Junior Member
Re: How size of structure is defined
The structure is packed. See #pragma pack for details.
-
Junior Member
Re: How size of structure is defined
Hi,
Hope it will give you clear information.
while allocating memory to member variables, first size of member variable which occupies maximum, is determined.
then allocation of memory, to each member variable goes in this block of size. below example will give more explaination.
considering normally memory required for
int = 4 bytes
char = 1 bytes
double = 8 bytes.
#include <stdio.h>
// Here memory allocated for member varaibles, is in terms of size of double(8 bytes).
typedef struct st1
{
int k; // memory allocated is, memory bloack size 8 bytes, but actaully aquired by 'k' is 4 bytes.
char c; // from previous memory block remaining 4 bytes, 1 byte is allocated for 'c', remaining 3 bytes padded.
double m;// from remaming 3 bytes of prevoius memory block, it cannot allocate needed 8 bytes, so it acquires another memory block(ie 8 bytes).
}myst1;
// Here memory allocated for member varaibles, is in terms of size of double(8 bytes).
typedef struct st2
{
int k; // memory allocated is, bloack size 8 bytes, but actaully aquired by 'k' is 4 bytes.
double m; // from remaming 4 bytes of prevoius memory block, it cannot allocate needed 8 bytes, so it acquires another memory block ie 8 bytes.
char c; // even though it needs only one byte, it has to be allocated in terms of memory block so it acquires another memory block ie 8 bytes.
}myst2;
void main()
{
myst1 str1;
myst2 str2;
int k= sizeof(str1);
printf("\n structer size = %d \n",k);
k= sizeof(str2);
printf("\n structer size = %d \n",k);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules