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


Groups > comp.lang.c > #148189

Re: sizeof object got by dereferencing uniniitalized pointer

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: sizeof object got by dereferencing uniniitalized pointer
Date 2019-11-28 15:51 +0100
Organization A noiseless patient Spider
Message-ID <qromtr$3o3$1@dont-email.me> (permalink)
References <21e377ab-2a49-4f7e-bc18-72f1cee63204@googlegroups.com> <9d5ee6d4-607b-405f-83d9-afbe2b073cac@googlegroups.com> <76a74489-4a1f-4561-a01b-b2962a10bae4@googlegroups.com>

Show all headers | View raw


On 28/11/2019 14:59, mark.bluemel@gmail.com wrote:
> On Thursday, 28 November 2019 13:37:52 UTC, Malcolm McLean  wrote:
>> On Thursday, 28 November 2019 13:20:59 UTC, V.Subramanian, India  wrote:
>>> Hi,
>>> I was trying to solve the questions related to pointers at the
>>> following site:
>>>
>>> https://www.geeksforgeeks.org/c-language-2-gq/advanced-pointer-c-gq/
>>>

I had a brief look at that page, and was not impressed.

>>>
>>> In this program, I thought sizeof(*p) is wrong because 'p' is not.
>>> initialized and we cannot dereference p.
>>>  However the answer was given as 800
>>>
>>> Am I wrong ? Please explain.

The key point here is that the operand to "sizeof" is not evaluated - it
is only the type that is important.  So since the address "p" is not
dereferenced, there is no problem.  Any C "tutorial" that talks about
"type-cast" is immediately suspect.  And beyond question 2, the
questions just get sillier and sillier as they leave behind all traces
of realism.  They are like trying to test mathematical ability by asking
for you to calculate 19482957156 / 927590, and then adding more digits
for the next question.

>>>
>> The sizeof keyword looks like a function, but it is in fact a built-in
>> operator. You are not really derefencing the pointer when you pass it *p,
>> you are telling it, at compile time, to take the size of the type p
>> points to.
> 
> In this case (when you are asking the size of an expression), you don't have
> to express it like a function.
> 
> #include <stdio.h>
> #define R 10
> #define C 20
> int main(){
>    int (*p)[R][C];
>    printf("%d\n",  sizeof *p);
>    return 0;
> }
> 
> Which is sightly less confusing.

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)".  Of course,
when learning C you need to understand both forms no matter which you
prefer to write.  (And I am fully aware that my view is not universally
held!)

> The other way to avoid confusion is to
> read a decent description of sizeof. 
> <https://docs.microsoft.com/en-us/cpp/c-language/sizeof-operator-c?view=vs-2019>
> isn't the standard, but it's clear and, I believe, accurate.
> 

Another fine source of reference is the "cppreference.com" site:

<https://en.cppreference.com/w/c/language/sizeof>

They try to be accurate to the standards, but are a bit easier to
navigate and they are clear about which versions of the standards are
relevant for each feature.

>> This is undesireable, and my view is that the form sizeof(*p) should be
>> avoided. 
> 
> It's perhaps worth mentioning that Malcolm's view isn't universally held.
> 

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