c - Does gcc initialize long strings to `""` but not short ones? -
note: i know reading uninitialized string undefined behvaiour. question gcc implementation.
i using gcc version 6.2.1 , have observed uninitialized strings of length greater 100 or initialized ""
. reading uninitialized string undefined behaviour, compiler free set ""
if wants to, , seems gcc doing when string long enough. of course never rely on behaviour in production code - curious behaviour comes in gcc. if it's not in gcc code somewhere it's strange coincidence keeps happening.
if write following program
/* string_initialization.c */ #include <stdio.h> int main() { char short_string[10]; char long_string[100]; char long_long_string[1000]; printf("%s\n", short_string); printf("%s\n", long_string); printf("%s\n", long_long_string); return(0); }
and compile , run gcc, get:
$ ./string_initialization �qe� $
(sometimes first string empty well). suggests if string long enough, gcc initialize ""
, otherwise not so.
if compile following program gcc , run it:
#include <stdio.h> int main() { char long_string[100]; int i; (i = 0 ; < 100 ; ++i) { printf("%d ", long_string[i]); } printf("\n"); return(0); }
then get
0 0 0 0 0 0 0 0 -1 -75 -16 0 0 0 0 0 -62 0 0 0 0 0 0 0 15 84 -42 -17 -4 127 0 0 14 84 -42 -17 -4 127 0 0 69 109 79 -50 46 127 0 0 1 0 0 0 0 0 0 0 -35 5 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -112 5 64 0 0 0 0 0 80 4 64 0 0 0 0 0 16 85 -42 -17
so start of string being initialized 0
, not whole thing.
i'd gcc source code see policy is, don't know code base enough know look.
background: cs student turned in work in declared string have length 1000 because 'otherwise strange symbols printed'. can guess why. want able give them answer why going on , why 'fix' worked.
update: of gave useful answers. i've found out computer prints out empty string if string of length 1000, garbage if string of length 960. see pts's answer explanation. of course, system-dependent , not part of gcc.
as others have commented before, reading uninitialized data (e.g. elements of short_string
) undefined behavior according c standard.
if interested in happens when compiling gcc , running on linux, here insights.
main
not first function gets run when program starts. entry point called _start
, , calls main
. on stack in these uninitialized arrays when main
running depends on has been put there before, i.e. _start
has done before calling main
. _start
depends on gcc , libc.
to figure out happens, may want compile program gcc -static -g
, , run in debugger, this:
$ gcc -static -g -o myprog myprog.c $ gdb ./myprog (gdb) b _start (gdb) run (gdb) s
instead of s
may want issue other gdb commands disassembly of _start
, , run instruction-by-instruction.
one possible explanation why program reading more 0
s uninitialized long array uninitialized short array, stack (mostly) 0s in beginning, before _start
started running, _start
has overwritten bytes of stack, beginning of long array in part of stack hasn't been overwritten _start
, it's still 0
s. use debugger confirm.
you may interested in reading data uninitialized global arrays. these arrays guaranteed initialized 0
c standard, , implemented gcc putting them .bss
section. see how .bss section not 0 initialized how .bss
initialized.
Comments
Post a Comment