12

While reading the Clang documentation, I came across the following intriguing tidbit: [1]

clang does not support the gcc extension that allows variable-length arrays in structures. This is for a few reasons: one, it is tricky to implement, two, the extension is completely undocumented, and three, the extension appears to be rarely used. Note that clang does support flexible array members (arrays with a zero or unspecified size at the end of a structure).

How can this extension be used? My understanding is that using alloca within a constructor causes the stack pointer to be restored at the end of the calling function, which in this case would be the constructor -- not at the end of the enclosing struct.

Thanks for the help!

osgx
  • 90,338
  • 53
  • 357
  • 513
void-pointer
  • 14,247
  • 11
  • 43
  • 61
  • 6
    That *is* wierd. http://ideone.com/qnghE – Robᵩ Aug 21 '12 at 16:11
  • I think you should look at http://stackoverflow.com/questions/1558025/c-initialize-array-within-structure – ForEveR Aug 21 '12 at 16:12
  • @forever No, that's an example of a C99 flexible array member. – void-pointer Aug 21 '12 at 16:21
  • @Rob_{\psi} Hm, I was hoping to be able to use the extension outside the scope of a function (e.g. in an allocator), so that the enclosing struct could actually own a pointer to the resource. If that's the only way the extension can be used, it sort of crushes my ambitions =( – void-pointer Aug 21 '12 at 16:28
  • @flolo Yes, I have read that documentation before. I think Clang in referring to the specific case which Rob_{\psi} is talking about (unless someone can demonstrate how the extension would be used outside a function). – void-pointer Aug 21 '12 at 16:38
  • @void-pointer: yes, you are right. I realized short after commenting, that they were indeed referring to that specific case and deleted my comment (for all who are interrested the link was: http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html ). – flolo Aug 21 '12 at 16:43
  • Given that C++ does not support VLAs at all, as a GCC extension to C++, it is likely safe to assume it is implemented using C semantics. VLAs were introduced in C99, where it is clear in Section 6.7.5.2 p2 that global VLAs are prohibited. C11 has similar language. – jxh Aug 21 '12 at 20:55
  • 3
    @user315052, C doesn't support VLAs as struct members either, so what are the C semantics? C supports "flexible array members" (6.7.2.1) which are not the same. – Jonathan Wakely Aug 23 '12 at 13:12
  • @JonathanWakely: You are right. – jxh Aug 23 '12 at 15:05

1 Answers1

7

See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37428

and also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42121

Yes, it's weird.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • PR 37428 from 2008-09 was fixed in gcc 4.9 at 2014-03-26 by Marek Polacek of RedHat with commit https://github.com/gcc-mirror/gcc/commit/c051a2940b45464f0bd42959def3a10c91bf688b (svn 208836, PR c/37428) with addition of one sentence and one example to "6.19 Arrays of Variable Length" https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Variable-Length.html: "As an extension, GCC accepts variable-length arrays as a member of a structure or a union. `void foo(int n) { struct S { int x[n]; }; }`". 6 years to confess existence of problem: http://thelinuxjedi.blogspot.com/2014/02/why-vlais-is-bad.html – osgx Mar 06 '17 at 19:52
  • @osgx it's not about _"confessing the existence of a problem"_, the bug was reported by a GCC dev, so the problem was "confessed" in 2008. As your link shows, the weird extension has been used in the real world, so simply removing support for it is difficult. – Jonathan Wakely Mar 07 '17 at 10:16