Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sizeof

Status
Not open for further replies.

nappaji

Programmer
Mar 21, 2001
76
US
struct TEST{
char a;
char b;
int c;
short d;
int *p;
};

when i do a sizeof(struct TEST), the value returned is 16.

But when i do a
sizeof (char) = 1
sizeof(int) =4;
sizeof(short) = 2;
sizeof(int *) =4;

so, should'nt the total size be , 1+1+4+2+4 = 12????

Please help.
 
Because some padded bytes are inserted.
To access memory efficiently ( and to avoid
bus error for serveral systems ), a structure
is aligned by the most significant size.

The most significant size of your structure is 4.
So, actual memory block of your structure is


[a][ ][ ] [c][c][c][c] [d][d][ ][ ] [p][p][p][p]

[ ] means 1 byte and if there is no character, it
is padded byte to be aligned at the 4 bytes boundary.
Count them, you can see it is 16 bytes!

Another consideration to reduce you structure size :
If your strcuture is defined like this,
struct TEST {
char a;
char b;
short d;
int c;
int *p;
};

Then it is 12 bytes because,

[a][d][d] [c][c][c][c] [p][p][p][p]

I hope it is helpful to you. Thanks
Hee S. Chung
heesc@netian.com
 
hmm, maybe hypertext cannot print [ b ]...

The first memory block is

(a)(b)( )( ) (c)(c)(c)(c) (d)(d)( )( ) (p)(p)(p)(p)

The second is

(a)(b)(d)(d) (c)(c)(c)(c) (p)(p)(p)(p)

I previewed them, so they are exact.
I'm sorry but this server doesn' accept [ b ].
:-( Hee S. Chung
heesc@netian.com
 
Yes... it is a padding issue and on a side note:

ignore in brackets [ignore] will allow the or any other tag to be ignored. i constantly have this problem with when demonstrating for loops and ending up with italics[/ignore]

Matt

 
Why not deselect Process TGML checkbox when using such things, it is in the bottom left of the textArea in which you type your Replies.
Regards,
SwapSawe.
 
Status
Not open for further replies.

Similar threads

  • Locked
  • Question
sizeof 1
Replies
2
Views
116
Replies
2
Views
114
  • Locked
  • Question
sizeof 1
Replies
8
Views
146

Part and Inventory Search

Sponsor

Back
Top