Re: [dev] [style] variable declaration locations and varriable length arrays vs malloc

From: Martti Kühne <mysatyre_AT_gmail.com>
Date: Tue, 3 Mar 2015 09:01:43 +0100

On Tue, Mar 3, 2015 at 12:56 AM, Anthony J. Bentley <anthony_AT_cathet.us> wrote:
> Evan Gates writes:
>> Declaring variables at the top of a block, as opposed to top of the
>> function has a few uses, but the most useful (in my limited
>> experience) is combining it with C99's variable length arrays to
>> create buffers without calls to malloc/free. For example:
>>
>> while ((d = readdir(dp))) {
>> char buf[strlen(path) + strlen(d->d_name) + 1];
>> ....
>> }
>
> VLAs are a fundamentally broken feature because they do not allow any
> error checking. alloca() is the same.
>


alloca() isn't even standard C, that's some black voodoo GNU sorcery
right there.
You should use one buffer, and that buffer's length is some PATH_MAX
or a balloonable realloc.

cheers!
mar77i
Received on Tue Mar 03 2015 - 09:01:43 CET

This archive was generated by hypermail 2.3.0 : Tue Mar 03 2015 - 09:12:07 CET