l1[]
and l2[]
are removed during compiler optimisation because both are local and unused variables
(no chance that both variable will be use any where else).
you can compile your code with -S
option to generate assembly code: and there is no definition for l1[]
and l2[]
in main not even any where else:
input file was x.c
, compiled with command gcc -S x.c
to generate assembly file x.s
main:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movb $28, -4(%ebp)
movb $44, -3(%ebp)
movb $60, -2(%ebp)
movb $76, -1(%ebp)
leave
ret
.size main, .-main
.data
.type l2.1250, @object
.size l2.1250, 4
But you can find definition for g1[] and g2[]
.
.file "x.c"
.globl g1
.data
.type g1, @object
.size g1, 4
g1:
.byte 26
.byte 42
.byte 58
.byte 74
.type g2, @object
.size g2, 4
g2:
.byte 27
.byte 43
.byte 59
.byte 75
.text
.globl main
.type main, @function
Additionally , It would be interesting to know if you compile if you compile you code with flag -O3
optimisation flag level 3 then only definition of g1[]
is present. and global static variables(private to the file) are also removed.
input file was x.c
, compiled with command gcc -S -O3 x.c
to generate assembly file x.s
below:
.file "x.c"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
popl %ebp
ret
.size main, .-main
.globl g1
.data
.type g1, @object
.size g1, 4
g1:
.byte 26
.byte 42
.byte 58
.byte 74
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
.section .note.GNU-stack,"",@progbits
g1[]
is global data only present, and g2[]
is removed in -O3
.
g2[]
use defined static unsigned char g2[]
so access only within this file and not use so again unused. But g1[]
is global it may be useful by other program if other file include it this. And compiler is not allowed to optimisation away global objects.
Reference: How do I prevent my 'unused' global variables being compiled out?
So, this all due to compiler optimisation!