Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.sizeof] Clarify sizeof(array) #4808

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jensmaurer
Copy link
Member

Fixes #4794

array. This implies that the size of an array of $n$ elements is
$n$ times the size of an element.
When applied to an array of $n$ elements,
the result is $n$ times the size of an element.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I know this is my wording suggestion, but reading it back in context: We start this paragraph by describing a special case for reference types that doesn't follow the general rule in p1 (and can't because there are no objects of reference type). Following that up with this might be taken as suggesting that this case is also overriding the general rule. Maybe we could move this rule into p1 so it doesn't seem to parallel the reference case?

More drastic suggestion: I wonder if perhaps this wording belongs in [intro.object] instead. Perhaps in paragraph 8 where we talk about objects occupying storage? We could split the second half of p8 into two:

  1. [...] Otherwise, the circumstances under which the object has zero size are implementation-defined.

8+. Unless it is a bit-field (11.4.10), an object with nonzero size shall occupy one or more bytes of storage, including every byte that is occupied in full or in part by any of its subobjects. An object of trivially copyable or standard-layout type (6.8) shall occupy contiguous bytes of storage. An object of array type shall not occupy any byte that is not occupied by any of its subobjects.[Footnote: This implies that sizeof(T[n]) == n * sizeof(T).]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the wording. Do we all agree that this is editorial?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's editorial; this is what the standard always said. If you're uncomfortable, let's punt this to "editorial with CWG oversight".

@zygoloid, your input solicited.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we think the rule for classes is? Is it:

  • an object of class type occupies its tail padding, therefore an object of array type does too
  • an object of class type doesn't occupy its tail padding but as a special case we increase the value produced by sizeof as described above

The first option seems to disallow tail padding reuse (by the "occupy disjoint bytes of storage" rule). The second option seems to mean that sizeof(T[n]) != n * sizeof(T).

I think the conclusion is probably that sizeof(T) can be larger than the number of bytes occupied by T, and this happens precisely when T has tail padding. :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the current wording doesn't allow an array element subobject to be potentially-overlapping (but it seems that the array itself is unintendedly allowed, which is meaningless)(CWG2527), and thus it exactly occupies all sizeof(T) bytes according to [expr.sizeof]/1.

IMO nothing is unclear in this PR, but I'm a bit confused that "number of bytes occupied by a non-potentially-overlapping object" is mentioned in [expr.sizeof] only, not in [intro.object].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jensmaurer Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a consistent model here, and fixing this is likely CWG material.

Previously, the odd subclause [expr.sizeof] normatively
required contiguous layout of arrays.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[expr.sizeof]p2 makes an unjustified inference about the size of an array
4 participants