Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 1
    A bit speculative (since this is a hammer looking for a nail), but perhaps alloca() would really outshine malloc() in a multithreaded environment because of the lock contention in the latter. But this is a real stretch since small arrays should just use a fixed size, and large arrays will probably need the heap anyway. Commented Mar 14, 2013 at 13:57
  • 1
    @chrisaycock Yes, very much hammer looking for a nail, but a hammer which actually exists (be it C99 VLA or the not-actually-in-any-standard alloca, which I think are basically same thing). But that multithreaded thing is good, editing question to include it! Commented Mar 14, 2013 at 14:06
  • One disadvantage of VLAs is that there's no mechanism for detecting an allocation failure; if there's not enough memory, the behavior is undefined. (The same is true for fixed-size arrays -- and for alloca().) Commented Mar 14, 2013 at 15:38
  • @KeithThompson Well, there's no guarantee that malloc/new detects allocation failure either, for example see Notes for Linux malloc man page (linux.die.net/man/3/malloc). Commented Mar 14, 2013 at 15:42
  • @hyde: And it's debatable whether Linux's malloc behavior conforms to the C standard. Commented Mar 14, 2013 at 18:53