c - argv prints out environment variables -


i experimenting randomly argc , argv in c, program(try.c):

/* trying understand argc , argv.*/  #include<stdio.h>  int main(int argc,char *argv[]) {   int i=0;    /*     argv[4]="arg4";     argv[5]="arg5";     argv[6]="arg6";     argv[7]="arg7";     argv[8]="arg8";     argv[9]="arg9";;   */     for(i=0;i<(argc+20);i++)    {        printf("arg %d: %s\n", i,argv[i]);    }    return 0; } 

when run as

./try arg1 arg2 arg3 

prints out this:

arg 0: ./try arg 1: arg1 arg 2: arg2 arg 3: arg3 arg 4: (null) arg 5: xdg_vtnr=7 arg 6: xdg_session_id=c2 arg 7: clutter_im_module=xim arg 8: selinux_init=yes arg 9: xdg_greeter_data_dir=/var/lib/lightdm-data/raman arg 10: gpg_agent_info=/run/user/1000/keyring-faajwi/gpg:0:1 arg 11: term=xterm arg 12: shell=/bin/bash arg 13: vte_version=3409 arg 14: windowid=58720268 arg 15: upstart_session=unix:abstract=/com/ubuntu/upstartsession/1000/1775 arg 16: gnome_keyring_control=/run/user/1000/keyring-faajwi arg 17: gtk_modules=overlay-scrollbar:unity-gtk-module arg 18: user=raman arg 19: ls_colors=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36: arg 20: xdg_session_path=/org/freedesktop/displaymanager/session0 arg 21: xdg_seat_path=/org/freedesktop/displaymanager/seat0 arg 22: ssh_auth_sock=/run/user/1000/keyring-faajwi/ssh arg 23: defaults_path=/usr/share/gconf/ubuntu.default.path` 

i expecting segmentation fault worked.it worked upto argc+63 , gives segmentation fault.i tried googling no success. please explain why happening i.e why environment variables(seems so) getting printed here? if decomment code, weirder results.

going outside limits of array leads undefined behavior. happens in reality, many unix-like systems (like linux) have third argument main function, array of string environment variable. complete prototype of main on such systems is

int main(int argc, char *argv[], char *envp[]) 

what when go out of bounds of argv array cross on environ array.

it should noted isn't in standard, it's there backwards compatibility old unix systems common.

it's mentioned in this reference, , documented in gnu libc manual.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -