TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Kurt Kuzba
date: 2003-08-31 08:56:00
subject: Bob`s questi

From: kkuzba{at}centurytel.net
To: c_echo{at}yahoogroups.com

* Author: Jasen Betts

JB>  I have a question: -  something that came up in the C++ echo
JB>  recently
JB>  Given
JB>  typedef struct moo
JB>  {
JB>    int foo;
JB>    char bar;
JB>  } moo;
JB>
JB>  is   sizeof(moo) * 100   always the same as
JB>  sizeof( moo[100] )
JB>  in other words does sizeof() include any slack space
JB>  required for alignment.

    I ran some tests using this code under MSVC++5.0, modifying it
 to give results for multiple structs as well as changing the size
 of the struct by manipulating the size of char array bar.

#include 

typedef struct{
 int foo;
 char bar;
}moo;
int main()
{
 char buf[16];
 moo milk;
 printf("size = %d\n", sizeof(milk));
 fgets(buf, 16, stdin);
 return 0;
}

[RESULT: 8]

    Increasing the char array bar to 4 elements, the size of the
 struct remained at 8 bytes.  Increasing it to 5 elements made
 the size of the struct 12 bytes.  The value of sizeof(milk)*100
 was equal to sizeof(milk) where milk was defined as an array having
 100 element. In my tests then, under MSVC5.0 at least, we may
 surmise that there is padding and it is taken into account by
 sizeof().  This may be dependent upon the compiler and environment
 in which you conduct such a test, however.

>  kkuzba{at}centurytel.net   http://home.centurytel.net/kkuzba
>  'It is to no place so fair that I shall lead you,' said Faramir.

--- SoupGate-Win32 v1.05
* Origin: kkuzba{at}centurytel.net (2:292/516.666)
SEEN-BY: 633/267 270
@PATH: 292/516 854 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™.