| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | sizeof() |
Hi Darin,
Lovely to hear from you again!
> #include
> typedef struct moo {
> int i;
> char c;
> } moo;
> int main()
> {
> printf("sizeof(moo) * 100 = %d\nsizeof(moo[100]) =
> %d\n",
> sizeof(moo) * 100, sizeof(moo[100]));
> exit(0);
> }
> [0] f:\tmp\a>gcc a.c -o a.exe
> [0] f:\tmp\a>a.exe
> sizeof(moo) * 100 = 800
> sizeof(moo[100]) = 800
>>> in other words does sizeof() include any slack space
>>> required for
>>> alignment.
> I'm not sure if there is any requirement on the
> compiler to do this. In fact, I would doubt it. Some
> platforms may decide to use that slack space for other
> things if there is no performance impact. Or if you
> give it a pragma. For example, with EMX (GCC for OS/
> 2), if I add #pragma pack(1), I get an output of 500
> for both.
> BB> Sizeof() returns the actual number of bytes of the dingus in the
> BB> brackets. Sizeof(moo) * 100 should return 300 with byte alignment.
> Nope - with 16-bit ints, it would probably return 400
> with byte alignment ;-)
With pragma pack 1 on the MSC V10 compiler it actually did return 300.
It had to or the data file wouldn't work at all :-)
> BB> The sizeof(an array of 100 structures of type moo) would also depend
> BB> on alignment. However unless you change alignment within the program
> BB> they should be the same.
> Somehow, my copy of the ANSI C standard eludes me, so
> I can't even look this up. I suspect it's correct,
> but can't confirm... :-/
I ran almost the identical program you typed in above on MSVC (on the
laptop), and the results were exactly the same, but like you I am not sure
compilers *must*. The only difference in the program snippets was that I
used two separate printf() statements. :-)
> BB> Darin McBride should be able to answer this off the top of his
> BB> head.
> A bit much confidence, methinks. ;-)
Darin if you don't know, there are few do. Reminds me of that old piece
of British doggerel:-
"The Duke of Buccleugh
didn't like whale steugh
but there are feugh
wheugh deugh!
deugh yeugh?"
Best Wishes,
Bill.
---
* Origin: Escan BBS (2:25/200)SEEN-BY: 633/267 270 @PATH: 25/200 108 252/110 250/501 140/1 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.