🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1126

Re: Question about sizeof in C/C++.

#1126
From: Steve Clamage
Date: Mon, 21 Dec 1992 16:58
39 lines
1960 bytes
jamesc@swapsdev.state.state.COM.AU (James Cribb) writes:

>Suppose a single struct Something needed 7 bytes of memory.  For word
>alignment, these might need to start on, say, 4-byte boundaries.

>E.g. (assume 4-byte floats),

>    struct Something
>    {
>        float  x;
>        char  s[3];
>    };

>Do the standards guarantee that sizeof(struct Something) will always be
>8 rather than 7?

The C Standard (there isn't a C++ Standard yet) guarantees:
- sizeof will return the amount of space reserved by the struct when it
    is a member of an array;
- each sub-object will be properly aligned;
- there will be no unused space (padding) at the start of the struct.

There may be padding between members or at the end of the struct.  The
size of the struct might be larger or smaller than the sum of the
sizeof's of its members.  (The compiler might pack struct members more
or less tightly than objects in an array.)

So if floats must start on a 4-byte boundary and sizeof(float)==4,
then sizeof(struct Something) cannot be 7.  It must be some value
greater than 7 which is a multiple of 4.  The most likely candidate
is 8, but that is not guaranteed by the Standard.

For the case of C++ structs which are also legal C structs, the rules
should be the same in the eventual C++ Standard.  That is, the intent
(at the moment) is that a C struct should have the same size and memory
layout when compiled by a compatible C++ compiler.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com
Technical Details
Message-ID: <1992Dec21.165858.8174@taumet.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!taumet!steve
References: <1992Dec3.195422.26393@cs.brown.edu> <1992Dec07.230924.20465@microsoft.com> <jamesc.724544278@bart> <jamesc.724907666@bart>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++,comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!taumet!steve
From: steve@taumet.com (Steve Clamage)
Subject: Re: Question about sizeof in C/C++.
Message-ID: <1992Dec21.165858.8174@taumet.com>
Organization: TauMetric Corporation
References: <1992Dec3.195422.26393@cs.brown.edu> <1992Dec07.230924.20465@microsoft.com> <jamesc.724544278@bart> <jamesc.724907666@bart>
Date: Mon, 21 Dec 1992 16:58:58 GMT
Lines: 39