Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #148209

Re: sizeof object got by dereferencing uniniitalized pointer

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: sizeof object got by dereferencing uniniitalized pointer
Date 2019-11-28 17:26 -0800
Organization A noiseless patient Spider
Message-ID <865zj38oc4.fsf@linuxsc.com> (permalink)
References (5 earlier) <c97a16bb-4384-5e83-2c2f-1099b62be653@hrosenau.de> <43093894-ed52-47de-85f4-eadd92a1499e@googlegroups.com> <7NTDF.74422$Jw6.61134@fx35.iad> <371f212f-1008-443a-ba0e-3c9acd732902@googlegroups.com> <cKXDF.34374$P82.23324@fx15.iad>

Show all headers | View raw


Richard Damon <Richard@Damon-Family.org> 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.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

sizeof object got by dereferencing uniniitalized pointer "V.Subramanian, India" <subramanian100in@yahoo.com> - 2019-11-28 05:20 -0800
  Re: sizeof object got by dereferencing uniniitalized pointer Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-28 05:37 -0800
    Re: sizeof object got by dereferencing uniniitalized pointer mark.bluemel@gmail.com - 2019-11-28 05:59 -0800
      Re: sizeof object got by dereferencing uniniitalized pointer David Brown <david.brown@hesbynett.no> - 2019-11-28 15:51 +0100
        Re: sizeof object got by dereferencing uniniitalized pointer Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-28 07:27 -0800
          Re: sizeof object got by dereferencing uniniitalized pointer Herbert Rosenau <nospam@hrosenau.de> - 2019-11-28 17:28 +0100
            Re: sizeof object got by dereferencing uniniitalized pointer Thiago Adams <thiago.adams@gmail.com> - 2019-11-28 08:51 -0800
              Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-28 17:36 +0000
                Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 16:37 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-29 01:27 +0000
                Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 17:47 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-28 21:16 -0500
                Re: sizeof object got by dereferencing uniniitalized pointer Keith Thompson <kst-u@mib.org> - 2019-11-29 00:38 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-29 09:03 -0500
                Re: sizeof object got by dereferencing uniniitalized pointer Keith Thompson <kst-u@mib.org> - 2019-11-29 13:15 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Keith Thompson <kst-u@mib.org> - 2019-11-29 13:38 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-29 07:09 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-29 06:10 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-29 11:12 -0500
                Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-12-01 09:32 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-29 07:39 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-29 22:00 +0000
                Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-29 22:40 -0500
                Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-30 11:19 +0000
                Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-30 09:01 -0500
                Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-12-01 10:28 -0800
              Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-28 12:49 -0500
                Re: sizeof object got by dereferencing uniniitalized pointer Thiago Adams <thiago.adams@gmail.com> - 2019-11-28 11:26 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-28 17:19 -0500
                Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 17:26 -0800
                Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-28 22:34 +0000
          Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-28 17:34 +0000
      You gotta wonder why people say things like this! (Was: sizeof object got by dereferencing uniniitalized pointer) gazelle@shell.xmission.com (Kenny McCormack) - 2019-11-28 17:19 +0000
    Re: sizeof object got by dereferencing uniniitalized pointer Keith Thompson <kst-u@mib.org> - 2019-11-28 14:55 -0800
      Re: sizeof object got by dereferencing uniniitalized pointer "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-11-30 15:28 -0800

csiph-web