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


Groups > comp.lang.c > #76720

Re: sizeof on an uninitialized pointer to a VLA

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: sizeof on an uninitialized pointer to a VLA
Date 2015-11-20 20:24 -0800
Organization None to speak of
Message-ID <lnd1v49dz5.fsf@kst-u.example.com> (permalink)
References (1 earlier) <n2ljik$iqo$1@dont-email.me> <ln610wevm8.fsf@kst-u.example.com> <87mvu8rbor.fsf@bsb.me.uk> <lnsi40axv9.fsf@kst-u.example.com> <dba734FjpqnU6@mid.individual.net>

Show all headers | View raw


Ian Collins <ian-news@hotmail.com> writes:
> Keith Thompson wrote:
[...]
>> Interesting point.  Here's an example:
>>
>>      #include <stdio.h>
>>
>>      int zero(void) {
>>          puts("zero()");
>>          return 0;
>>      }
>>
>>      int main(void) {
>>          int n = 10;
>>          char vla[n][n];
>>          printf("sizeof vla[zero()] = %zu\n", sizeof vla[zero()]);
>>      }
>>
>> The output using every version of gcc and clang I've tried is:
>>
>>      zero()
>>      sizeof vla[zero()] = 10
>>
>> The sizeof computation is correct, but it *could* have been determined
>> without calling the function.  As far as I can tell, the value returned
>> by the call to zero() can have no effect on the result of the sizeof
>> computation.
>
> Is this a case of the observer effect?
>
> I tired you code with Sun c99 and the whether the puts() line was there 
> or not, the immediate value 10 was pushed.

I'm not sure what you mean by "pushed".  Are you looking at the
generated code?

I tried the example with a rather old Sun C compiler.  The output was:

    sizeof vla[zero()] = 10

(Which IMHO *should* be correct, but the call to zero() generated by gcc
and clang follows what the standard says.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

sizeof on an uninitialized pointer to a VLA John Bode <jfbode1029@gmail.com> - 2015-11-19 11:39 -0800
  Re: sizeof on an uninitialized pointer to a VLA supercat@casperkitty.com - 2015-11-19 12:09 -0800
  Re: sizeof on an uninitialized pointer to a VLA "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2015-11-19 21:40 +0000
    Re: sizeof on an uninitialized pointer to a VLA supercat@casperkitty.com - 2015-11-19 13:50 -0800
  Re: sizeof on an uninitialized pointer to a VLA James Kuyper <jameskuyper@verizon.net> - 2015-11-19 17:50 -0500
    Re: sizeof on an uninitialized pointer to a VLA supercat@casperkitty.com - 2015-11-19 15:31 -0800
    Re: sizeof on an uninitialized pointer to a VLA Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-20 01:09 +0000
      Re: sizeof on an uninitialized pointer to a VLA supercat@casperkitty.com - 2015-11-19 19:10 -0800
    Re: sizeof on an uninitialized pointer to a VLA Keith Thompson <kst-u@mib.org> - 2015-11-20 03:52 -0800
      Re: sizeof on an uninitialized pointer to a VLA Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-20 14:24 +0000
        Re: sizeof on an uninitialized pointer to a VLA supercat@casperkitty.com - 2015-11-20 10:25 -0800
        Re: sizeof on an uninitialized pointer to a VLA Keith Thompson <kst-u@mib.org> - 2015-11-20 18:29 -0800
          Re: sizeof on an uninitialized pointer to a VLA Ian Collins <ian-news@hotmail.com> - 2015-11-21 16:35 +1300
            Re: sizeof on an uninitialized pointer to a VLA Keith Thompson <kst-u@mib.org> - 2015-11-20 20:24 -0800
              Re: sizeof on an uninitialized pointer to a VLA Ian Collins <ian-news@hotmail.com> - 2015-11-21 17:38 +1300
                Re: sizeof on an uninitialized pointer to a VLA Keith Thompson <kst-u@mib.org> - 2015-11-20 21:08 -0800
          Re: sizeof on an uninitialized pointer to a VLA Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-21 10:46 +0000
            Re: sizeof on an uninitialized pointer to a VLA Keith Thompson <kst-u@mib.org> - 2015-11-21 03:26 -0800
              Re: sizeof on an uninitialized pointer to a VLA Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-21 12:46 +0000
                Re: sizeof on an uninitialized pointer to a VLA Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-21 14:01 +0000
              Re: sizeof on an uninitialized pointer to a VLA supercat@casperkitty.com - 2015-11-21 09:02 -0800
                Re: sizeof on an uninitialized pointer to a VLA Keith Thompson <kst-u@mib.org> - 2015-11-21 12:09 -0800

csiph-web