about arrays initialization

uulinux uulinux@gmail.com
Sun Aug 28 00:57:00 GMT 2011


于 2011/8/27 下午 10:40, Maciej Bliziński 写道:
> 2011/8/27 uulinux<uulinux@gmail.com>:
>> localhost array # gcc -o main main.c
>> localhost array # ./main
>>         1       2       3       -1218524971     -944870872
>> ===============================================
>>
>>    I want to know, what is wrong with it?
>
> I'm not an expert, but it seems to me that in the expression "int
> a[5]={1, 2, 3, a[2], a[0]+a[1]};" when you say e.g. a[2], you are
> referring an uninitialized array. Your code, as far as initialization
> goes, would be equivalent to:
>
> int b[5];
> int a[5]={1, 2, 3, b[2], b[0]+b[1]};
>
> In this example, it's clear what's wrong.  Your code has the same
> problem, but slightly obfuscated.
>
> If you try to do the same in Python, you get:
>
>>>> a = [1, 2, 3, a[2], a[0]+a[1]]
> Traceback (most recent call last):
>    File "<stdin>", line 1, in<module>
> NameError: name 'a' is not defined
>
> Which is exactly the same problem you get in C, but exposed better.
>
> Maciej

Dear Maciej:

Thanks very mach. I am so glad to get a replay. I seem to know what you 
mean. The auto variable allocated from stack, whose value is initialized 
by code in runtime, not by compiler.

But look the assembler code compiled by gcc-4.4.5.
=========================================================
	.file	"main.c"
	.section	.rodata
.LC0:
	.string	"\t%d"
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl	%esp, %ebp
	andl	$-16, %esp
	subl	$48, %esp
	movl	32(%esp), %eax
	movl	24(%esp), %ecx
	movl	28(%esp), %edx
	leal	(%ecx,%edx), %edx
	movl	$1, 24(%esp)
	movl	$2, 28(%esp)
	movl	$3, 32(%esp)
	movl	%eax, 36(%esp)
	movl	%edx, 40(%esp)
	movl	$0, 44(%esp)
	movl	$0, 44(%esp)
	jmp	.L2
.L3:
	movl	44(%esp), %eax
	movl	24(%esp,%eax,4), %edx
	movl	$.LC0, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	addl	$1, 44(%esp)
.L2:
	cmpl	$4, 44(%esp)
	jle	.L3
	movl	$10, (%esp)
	call	putchar
	movl	$0, %eax
	leave
	ret
	.size	main, .-main
	.ident	"GCC: (Gentoo 4.4.5 p1.2, pie-0.4.5) 4.4.5"
	.section	.note.GNU-stack,"",@progbits
=========================================================
    Please notice this code compiled from "int a[5] = [1, 2, 3, a[2], 
a[0]+a[1]]":
	movl	$1, 24(%esp)
	movl	$2, 28(%esp)
	movl	$3, 32(%esp)
	movl	%eax, 36(%esp)
	movl	%edx, 40(%esp)
    I just doubt whether there is something wrong or not very good. I 
expect code as following:
	movl	$1, 24(%esp)
	movl	$2, 28(%esp)
	movl	$3, 32(%esp)
	movl	32(%esp), %eax
	movl	%eax, 36(%esp)
	movl	24(%esp), %edx
	movl	28(%esp), %eax
	leal	(%edx,%eax), %eax
	movl	%eax, 40(%esp)
    And I expect "int a[5] = [1, 2, 3, a[2], a[0]+a[1]]" is equivalent to:
         int a[5] = [1, 2, 3];
         a[3] = a[2];
         a[4] = a[0]+a[1];
    If the array is global variable and which will be put in .data 
segment by compiler, gcc-4.4.5 will report error as following. But I 
think this kind method of initialization is useful.
=========================================================
localhost array # gcc -o main main.c
main.c:28: error: initializer element is not constant
main.c:28: error: (near initialization for 'a[3]')
main.c:28: error: initializer element is not constant
main.c:28: error: (near initialization for 'a[4]')
=========================================================



More information about the Gcc-help mailing list