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

Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • 3
    FWIW VLA's are not standard in C++ so anything you get is going to be implementation defined. – NathanOliver Jul 19 '17 at 14:43
  • 2
    http://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p18 – Eugene Sh. Jul 19 '17 at 14:46
  • 5
    @Olaf Why did you remove the C++ tag? The question is "C vs C++". Isn't that legal or reasonable? – Scheff's Cat Jul 19 '17 at 14:51
  • 1
    A simple look into the standard would have saved time. – too honest for this site Jul 19 '17 at 14:52
  • 1
    @Scheff Apparently the question applies only to one of the different languages. – too honest for this site Jul 19 '17 at 14:53
  • 1
    @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++ . – Eugene Sh. Jul 19 '17 at 14:54
  • @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. – Scheff's Cat Jul 19 '17 at 14:59
  • 2
    It conflicts with the question explicitly asking about differences between C and C++ (and the answer addresses this) -> tag restored. –  Jul 19 '17 at 15:01
  • Oje... (Peace, please.) – Scheff's Cat Jul 19 '17 at 15:02
  • 1
    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. – Timmmm Jul 19 '17 at 15:02
  • 1
    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 `printf` – Chris Turner Jul 19 '17 at 15:03
  • @Scheff: Must be some German thingie … – too honest for this site Jul 19 '17 at 15:03
  • 3
    @ChrisTurner: Because C/C++ is full of undefined and implementation-defined behaviour. `printf` won't tell you that. – Timmmm Jul 19 '17 at 15:03
  • 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. –  Jul 19 '17 at 16:02
  • @Timmmm: We can only judge from what was presented in the question. And from that it is evident you did no research at all. – too honest for this site Jul 19 '17 at 16:03
  • I don't think it's an exact dupe as this question address c++ along with c tag. Reopened it. – haccks Jul 19 '17 at 16:23
  • BE sure to review this find [answer](https://stackoverflow.com/a/44870679/2410359) to [Flexible array members can lead to undefined behavior?](https://stackoverflow.com/q/44745677/2410359) – chux - Reinstate Monica Jul 19 '17 at 17:46

1 Answers1

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

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    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). – Timmmm Jul 19 '17 at 15:05
  • 1
    @felix: `struct { char a; double d[]; };`. If you ignored `d`, the size could be 1. – rici Jul 19 '17 at 16:23
  • 1
    @rici yes, that's a simple one, but [have a look at the discussion here](https://stackoverflow.com/q/44745677/2371524) ;) (I just see I might have confused this with some other quotes... sorry) –  Jul 19 '17 at 16:27
  • @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... – rici Jul 19 '17 at 18:59
  • ... 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? – rici Jul 19 '17 at 19:05
  • @rici 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. –  Jul 19 '17 at 19:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149647/discussion-between-rici-and-felix-palmen). – rici Jul 19 '17 at 20:57