6

Given

struct Foo {
    uint32_t a;
    uint32_t b[];
};

What is sizeof(Foo)? Is it implementation-defined or undefined behaviour? Does the answer differ for C vs C++?

17
  • 3
    FWIW VLA's are not standard in C++ so anything you get is going to be implementation defined. Commented Jul 19, 2017 at 14:43
  • 2
    port70.net/~nsz/c/c11/n1570.html#6.7.2.1p18 Commented Jul 19, 2017 at 14:46
  • 5
    @Olaf Why did you remove the C++ tag? The question is "C vs C++". Isn't that legal or reasonable? Commented Jul 19, 2017 at 14:51
  • 2
    It conflicts with the question explicitly asking about differences between C and C++ (and the answer addresses this) -> tag restored. Commented Jul 19, 2017 at 15:01
  • 3
    @ChrisTurner: Because C/C++ is full of undefined and implementation-defined behaviour. printf won't tell you that. Commented Jul 19, 2017 at 15:03

1 Answer 1

7

The compiler will ignore the flexible array member as it were not there.

C11-§6.7.2.1 (p18)

[...] In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply [...].

AFAIK, flexible array member is not the part of C++ standard till c++14. But, GNU support it as an extension. Behaviour will be similar for both C and C++.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! I actually can't find any reference to it in C++14, but I think it is safe to assume that Clang/GCC/etc apply the same behaviour to it in C++ as they do in C (and by experiment they seem to).
@felix: struct { char a; double d[]; };. If you ignored d, the size could be 1.
@rici yes, that's a simple one, but have a look at the discussion here ;) (I just see I might have confused this with some other quotes... sorry)
@felix I saw that discussion earlier but chose to avoid entering the fray. The C Standard is not in the business of deliberately building false expectations, nor is it a kind of Delphic Oracle whose mysterious pronouncements can only be unravelled by high priests skilled in the magic arts. Rather, it is a description of a programming language intended to be used (and to be usable). A feature described by the standard must be usable; otherwise it would not be there. If the wording makes that impossible, it is a defect and should be corrected; it won't be the first nor the last such error...
... The question that you link to struck me as a misappropriation of energy: if OP's argument is valid, it should be presented as a DR to the standardisation committee. Using it instead to discourage the use of FAMs strikes me as a tactic by someone whose aesthetic sensibilities are offended by FAMs, and who therefore believes that they should be extirpated from the language. (A serious argument to that effect would also better be submitted to the committee but I expect it would not prosper.) Now: what interesting example were you referring to?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.