Re: [PATCH 1/6] New function null_stream

Luis Machado
2017-01-18T16:36:19Z

On 01/18/2017 09:54 AM, Simon Marchi wrote:
> On 2017-01-18 10:28, Luis Machado wrote:
>> That is perfectly fine. The cryptic bit i was referring to was
>> declaring/initializing a static variable inside this particular
>> function.
>
> Indeed, and I pointed to Pedro's patch because it removes that. The
> global object is statically allocated and is constructed at startup, so
> there's no more checking if it's initialized nor static variable inside
> a function.
>
>> It ought to be possible to initialize the static variable somewhere
>> else and only do the null check/allocation in the function? Compilers
>> will often zero these out too, so initializing to NULL may not even be
>> needed?
>
> I think this is a common pattern when you want to have a singleton with
> lazy initialization, but I agree that it can be confusing. The
> variables with static storage duration are put in .bss (since they are
> not in .data), so yes they'd be initialized to 0 automatically, I
> believe. I prefer to see the = NULL though.
>
> Of course we could move the variable outside the scope of the function,
> but then it would be possible to reference it from other functions. The
> goal of declaring it in the function is that the only way to reach it is
> by calling the function it's in.
>
>> The fact that the initialization only happens once but the source line
>> is there forever can cause some confusion.
>
> Well, you can thank Mr Ritchie & friends I suppose :).
>

That's fine. If we're sticking with it, i'd at least add a comment for
the sake of making it more obvious what the intentions for such a
variable are and the fact it will outlive the function itself.