What is the size of a struct with a flexible array member? [duplicate]
Asked Answered
B

1

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++?

Breena answered 19/7, 2017 at 14:43 Comment(17)
FWIW VLA's are not standard in C++ so anything you get is going to be implementation defined.Mahican
port70.net/~nsz/c/c11/n1570.html#6.7.2.1p18Bison
@Olaf Why did you remove the C++ tag? The question is "C vs C++". Isn't that legal or reasonable?Fix
A simple look into the standard would have saved time.Farm
@Scheff Apparently the question applies only to one of the different languages.Farm
@Scheff I think it is not illegal. But it is better to have two different question as there are many C experts here and many C++ experts which are not always the same people. The C++ part was the reason I put the link to the C standard in the comment and not made it an answer as I don't have it from the top of my mind about C++ .Bison
@Olaf For me, that was not so "apparently" (though my stomach had me prevented to do something like this in C++) but I see your intention.Fix
It conflicts with the question explicitly asking about differences between C and C++ (and the answer addresses this) -> tag restored.Fishhook
Oje... (Peace, please.)Fix
It works in C++ in both Clang and GCC so the question can be answered. The answer might be "It is not in the standard, but supported but GCC and Clang. On those compilers the size is 4". That's useful information.Breena
Not even sure why the question needs to be asked - it's easy enough to find out what the sizeof would be with a simple call to printfCella
@Scheff: Must be some German thingie …Farm
@ChrisTurner: Because C/C++ is full of undefined and implementation-defined behaviour. printf won't tell you that.Breena
Yes, better don't judge the quality of the question this way. The standard documents aren't exactly an easy reading, you have to be already very familiar with them in order to find what you're looking for. And apparently, the question is well received. The duplicate isn't a perfect one, but as it has been answered that FAMs just don't exist in C++, it's ok as a link to a strongly related question.Fishhook
@Timmmm: We can only judge from what was presented in the question. And from that it is evident you did no research at all.Farm
I don't think it's an exact dupe as this question address c++ along with c tag. Reopened it.Poon
BE sure to review this find answer to Flexible array members can lead to undefined behavior?Denudation
P
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++.

Poon answered 19/7, 2017 at 14:47 Comment(7)
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).Breena
@felix: struct { char a; double d[]; };. If you ignored d, the size could be 1.Earpiece
@Earpiece 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)Fishhook
@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...Earpiece
... 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?Earpiece
@Earpiece As I understood his comments, he did prepare a DR. But I'm not sure his argumentation holds ;) The passage in this answer just reminded me having seen this discussion.Fishhook
Let us continue this discussion in chat.Earpiece

© 2022 - 2024 — McMap. All rights reserved.