From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 33496 invoked by alias); 18 Jan 2017 16:36:19 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 33486 invoked by uid 89); 18 Jan 2017 16:36:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=theyd, they'd, Hx-languages-length:1712, sticking X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Jan 2017 16:36:16 +0000 Received: from svr-orw-mbx-03.mgc.mentorg.com ([147.34.90.203]) by relay1.mentorg.com with esmtp id 1cTtDa-00011R-Er from Luis_Gustavo@mentor.com ; Wed, 18 Jan 2017 08:36:14 -0800 Received: from [172.30.8.199] (147.34.91.1) by svr-orw-mbx-03.mgc.mentorg.com (147.34.90.203) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Wed, 18 Jan 2017 08:36:10 -0800 Reply-To: Luis Machado Subject: Re: [PATCH 1/6] New function null_stream References: <1484051178-16013-1-git-send-email-yao.qi@linaro.org> <1484560977-8693-1-git-send-email-yao.qi@linaro.org> <1484560977-8693-2-git-send-email-yao.qi@linaro.org> <20170118144506.GM28060@E107787-LIN> <79d15965-fd1c-5cff-54f1-d6b7d2e6b884@codesourcery.com> <2b52597c046b688cca11d55ac12cfb04@polymtl.ca> <70308951-95e4-a628-0a71-1526f2963640@codesourcery.com> <25ec0b5e-0d11-35c7-099a-243284c6be6f@codesourcery.com> To: Simon Marchi CC: Yao Qi , From: Luis Machado Message-ID: <24c13bd9-839a-0347-a18d-9f14ef3fd4dc@codesourcery.com> Date: Wed, 18 Jan 2017 16:36:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: svr-orw-mbx-02.mgc.mentorg.com (147.34.90.202) To svr-orw-mbx-03.mgc.mentorg.com (147.34.90.203) X-IsSubscribed: yes X-SW-Source: 2017-01/txt/msg00361.txt.bz2 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.