Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 29 May 2002 17:54:09 -0400
From:      Garance A Drosihn <drosih@rpi.edu>
To:        Marcel Moolenaar <marcel@xcllnt.net>
Cc:        cvs-committers@FreeBSD.org, cvs-all@FreeBSD.org
Subject:   Re: cvs commit: src/sys/sys gpt.h
Message-ID:  <p05111709b91af6b2c4d8@[128.113.24.47]>
In-Reply-To: <20020529131927.C64995@kayak.xcllnt.net>
References:  <200205290258.g4T2wgF83137@freefall.freebsd.org> <9551.1022648961@critter.freebsd.dk> <20020529053653.GA306@dhcp01.pn.xcllnt.net> <20020529173621.B11817@chiark.greenend.org.uk> <20020529131927.C64995@kayak.xcllnt.net>

next in thread | previous in thread | raw e-mail | index | archive | help
At 1:19 PM -0700 5/29/02, Marcel Moolenaar wrote:
>On Wed, May 29, 2002 at 05:36:21PM +0100, Tony Finch wrote:
>  > It's required for correct alignment of the uint64_t members
>  > when you create an array of the struct.
>
>I think it's a mistake

answer, part 1:
Well, I also think the C language has several mistakes in it.
However, the C language is defined as it is.  We are not going
to change the definition of C by arguing about it here!

>                 to make the padding between elements in an
>array part of the type of element. An int32_t followed by
>an int64_t in a struct also doesn't cause the int32_t to
>have a 64-bit size to handle the alignment of the int64_t
>that follows it.

You are correct that it does not change the size of the
variable.  However, it DOES change the size of the struct
which will hold those two variables.  Similarly, an int64_t
followed by an int32_t will not change the size of either
of those two variables.  It is only the size of the area
needed to hold both of those variables which will change
due to the alignment needed for the int64_t.

>It's now totally impossible to answer trivial questions like
>
>	what's the size of struct X { int64_t a; int32_t b; };
>
>There's no value in an answer that starts of with "It depends...".

It is simple to answer that question.  Use 'sizeof()'.  The
answer will always be the same, for a given struct.

>The size should always be 12 bytes, independent of any alignment
>requirements I may attach to it.

What you're really arguing is that you do not like the answer
it is giving you, which is a different issue than saying the
answer "depends" on some deep dark brooding mystery.

So, what should be the size of:
          struct Y { int32_t c; int64_t d; };
?  Should that be 12, just because you want it to be 12?  or
does "it depend" on the alignment of the variables within that
struct?  Adding up the sizeof() each variable in Y comes to the
same number as adding up the sizeof() each variable in X.  Why
should sizeof() ignore alignment in struct X and not in struct Y?

Well, I don't mean to make a big deal of this.  Mainly I just
wanted to point out the mistake in your earlier argument (the
one where you talked about *variables* changing size, when the
issue is really the *struct* to holds those variables).  You may
not like the answer from sizeof(struct), but C is not arbitrarily
changing the size of any of the variables inside of a struct.

-- 
Garance Alistair Drosehn            =   gad@gilead.netel.rpi.edu
Senior Systems Programmer           or  gad@freebsd.org
Rensselaer Polytechnic Institute    or  drosih@rpi.edu

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?p05111709b91af6b2c4d8>