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


Groups > comp.lang.c > #76721

Re: sizeof on an uninitialized pointer to a VLA

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: sizeof on an uninitialized pointer to a VLA
Date 2015-11-21 17:38 +1300
Message-ID <dbaappFjpqnU7@mid.individual.net> (permalink)
References (2 earlier) <ln610wevm8.fsf@kst-u.example.com> <87mvu8rbor.fsf@bsb.me.uk> <lnsi40axv9.fsf@kst-u.example.com> <dba734FjpqnU6@mid.individual.net> <lnd1v49dz5.fsf@kst-u.example.com>

Show all headers | View raw


Keith Thompson wrote:
> 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?

Yes.

> 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.)

If wee look at the gcc output (with -01), with puts():

main:
	leal	4(%esp), %ecx
	andl	$-16, %esp
	pushl	-4(%ecx)
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ecx
	subl	$4, %esp
	call	zero
	subl	$8, %esp
	pushl	$10
	pushl	$.LC1
	call	printf
	addl	$16, %esp
	movl	-4(%ebp), %ecx
	leave
	leal	-4(%ecx), %esp
	ret

Without:

main:
	leal	4(%esp), %ecx
	andl	$-16, %esp
	pushl	-4(%ecx)
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ecx
	subl	$12, %esp
	pushl	$10
	pushl	$.LC0
	call	printf
	addl	$16, %esp
	movl	-4(%ebp), %ecx
	leave
	leal	-4(%ecx), %esp
	ret

So without puts(), zero() isn't called.  Even with it, the return value 
isn't used.

-- 
Ian Collins

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