Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++,comp.lang.c Subject: Re: alloca()-support Date: Mon, 30 Sep 2019 11:13:00 -0700 Organization: None to speak of Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="2560081c10b217bbe74b090b0e11513c"; logging-data="12004"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QPzRoK0gXZcJH82BTI5b+" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:8wC9rppqLyaD+xeCH85TPJckAxk= sha1:g6GQYhJcCSwhIKxo0J26Kt+94fk= Xref: csiph.com comp.lang.c++:68812 comp.lang.c:146463 Soviet_Mario writes: > On 30/09/2019 18:58, Bonita Montero wrote: >>>>> Since when (if at any time) was variable size automatic >>>>> array supported ? >> >>>> Since C99. >> >>> for curiosity, this feature has one or more of the >>> limitations I feared ... or even none of them ? >> >> Yes, you could run out of stack-space, maybe hitting a >> guard-page >> in the best case so that you wouldn't corrupt any other data. > > uhm, I didn't mean THAT kind of risk (unavoidable), but the > possible difficulties in defining variables beyond the point > of declaration of the variable-size array. > > I can't figure out how the compiler would reference them in > standard efficient ways (use of registers). > > Anyway, the difference with alloca () would be in a safer > management of possible collisions and running out of stack > space ? (compared with the plain declarations) ? No. Neither VLAs nor alloca() provide any protection against running out of space. Both have undefined behavior if there isn't enough memory for the allocation. alloca() (which, again, is non-standard) is a function that returns a pointer. It has no provision to return a null pointer on failure. Defining a VLA, on the other hand, creates an array object to which you can meaningfully apply sizeof. Also, space allocated by alloca() is deallocated on return from the enclosing function, not at the end of the enclosing block. Since alloca() is a function, it can be invoked in contexts where a VLA can't be created. If you call alloca() as part of a function argument, it's likely to corrupt the stack. VLAs were introduced in C99 and made optional in C11 and are not supported, except perhaps as an extension, in C++. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */