Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: sizeof object got by dereferencing uniniitalized pointer Date: Thu, 28 Nov 2019 17:26:35 -0800 Organization: A noiseless patient Spider Lines: 70 Message-ID: <865zj38oc4.fsf@linuxsc.com> References: <21e377ab-2a49-4f7e-bc18-72f1cee63204@googlegroups.com> <9d5ee6d4-607b-405f-83d9-afbe2b073cac@googlegroups.com> <76a74489-4a1f-4561-a01b-b2962a10bae4@googlegroups.com> <2119c5dc-fc01-4a74-93f4-8027c7cc210e@googlegroups.com> <43093894-ed52-47de-85f4-eadd92a1499e@googlegroups.com> <7NTDF.74422$Jw6.61134@fx35.iad> <371f212f-1008-443a-ba0e-3c9acd732902@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="81b92e3fa8397acac1ab5cf195ff75cd"; logging-data="3555"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180OHOwLvCRnAmen/E3dssyUNSfsHZWkec=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:WFzvIqfvBVuPMrhXLFASbPVP+Wc= sha1:OpEVssxaF8k25PDZvsmT35MYYjM= Xref: csiph.com comp.lang.c:148209 Richard Damon writes: > On 11/28/19 2:26 PM, Thiago Adams wrote: > >> On Thursday, November 28, 2019 at 2:50:04 PM UTC-3, Richard Damon wrote: >> >>> On 11/28/19 11:51 AM, Thiago Adams wrote: >>> >>>> On Thursday, November 28, 2019 at 1:28:13 PM UTC-3, Herbert Rosenau wrote: >>>> >>>>> Am 28.11.19 um 16:27 schrieb Malcolm McLean: >>>>> >>>>>> On Thursday, 28 November 2019 14:51:48 UTC, David Brown wrote: >>>>>>> >>>>>>> >>>>>>> Personally, I dislike writing "sizeof *p" - to me, it looks like a >>>>>>> multiplication. I'd rather have it looking like a function, even though >>>>>>> it is not really like a function - I'd write "sizeof(*p)". >>>>>> >>>>>> Also, you frequently need the size of an array of objects. So the >>>>>> form >>>>>> >>>>>> sizeof *p * N >>>>>> >>>>>> is very common. >>>>> >>>>> sifeof p /* gives the size of p, the pointer p can point to */ >>>>> sizeof *p /* gives the size of a type p can point to */ >>>> >>>> Only when applied to variably types, sizeof is in runtime. >>>> >>>> int n = 10; >>>> >>>> int(*buf)[n] = malloc(sizeof(int) * n); >>>> >>>> printf("sizeof buf=%d", (int)(sizeof * buf)); //prints 40 >>> >>> But you could also have had >>> >>> int n = 10 >>> >>> int (*buf)[n] = 0 >>> >>> and sizeof *buf would still be 40 >>> >>> buf is NOT evaluated. >> >> I think, internally it creates a new variable that is initialized >> when the array is created. Let's say n2 is created and it assumes >> the value of n that is 10. if you change n still 10. >> the initialization is in runtime and the value is read from some >> internal variable. > > Yes, typically the creation of a variable sized type will create some > code to save the variable information about the type in some manner (it > might get optimized away if the information is available for any needed > range is available from other sources. > > The sizeof may need to access that information to compute the size of > that variably modified type, so it will likely have code, but it doesn't > need the variable to have a 'good' value, [...] Strictly speaking, if the variable used does not have a 'good' value (which 'buf' doesn't in this case), the result of doing the sizeof has undefined behavior. As a practical matter that is unlikely to have any ill effects, but as far as the letter of the law goes 'sizeof *buf' here is a programming transgression. The operand of sizeof is evaluated, with all the requirements that go with that, when the operand has a type that is a variable length array type, which is the case for the operand '*buf' here.