argv prints out environment variables
up vote
2
down vote
favorite
I was experimenting randomly with argc and argv in c, however this program(try.c):
/* Trying to understand argc and 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: %sn", 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 was expecting a segmentation fault but it worked.It worked for upto argc+63 and then gives segmentation fault.I tried googling but with no success. Someone please explain why is this happening i.e why are the environment variables(seems so) getting printed here?
If I decomment the code, I get even weirder results.
c linux
|
show 2 more comments
up vote
2
down vote
favorite
I was experimenting randomly with argc and argv in c, however this program(try.c):
/* Trying to understand argc and 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: %sn", 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 was expecting a segmentation fault but it worked.It worked for upto argc+63 and then gives segmentation fault.I tried googling but with no success. Someone please explain why is this happening i.e why are the environment variables(seems so) getting printed here?
If I decomment the code, I get even weirder results.
c linux
1
If you pass only 3 arguments tomain
but read upto 20 then you have undefined behaviour.
– P.P.
Jun 25 '15 at 8:43
4
If thou art indexeth beyond ye ole end of the array, why are you expecting a segfault? It's undefined behavior, anything can happen. By the way, some non-conforming implementations (I know of Apple doing this) have a 3-argumentmain()
like this:int main(int argc, char *argv, char *envp)
where the 3rd argument is an array of environment variables. They might happen to come after argv on the stack or something.
– The Paramagnetic Croissant
Jun 25 '15 at 8:44
Anything can happen, anything!? Like Cthulhu leaping out of a black hole, or my bank account filling up with millions of worthless US Confederate dollars? Hell, we should try this more often just to see what happens.
– Paul
Jun 25 '15 at 8:48
@Paul Don't forget the nasal demons!
– Some programmer dude
Jun 25 '15 at 8:50
1
@SouravGhosh I would love to!
– The Paramagnetic Croissant
Jun 25 '15 at 9:21
|
show 2 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I was experimenting randomly with argc and argv in c, however this program(try.c):
/* Trying to understand argc and 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: %sn", 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 was expecting a segmentation fault but it worked.It worked for upto argc+63 and then gives segmentation fault.I tried googling but with no success. Someone please explain why is this happening i.e why are the environment variables(seems so) getting printed here?
If I decomment the code, I get even weirder results.
c linux
I was experimenting randomly with argc and argv in c, however this program(try.c):
/* Trying to understand argc and 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: %sn", 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 was expecting a segmentation fault but it worked.It worked for upto argc+63 and then gives segmentation fault.I tried googling but with no success. Someone please explain why is this happening i.e why are the environment variables(seems so) getting printed here?
If I decomment the code, I get even weirder results.
c linux
c linux
asked Jun 25 '15 at 8:42
Raman
1,5681232
1,5681232
1
If you pass only 3 arguments tomain
but read upto 20 then you have undefined behaviour.
– P.P.
Jun 25 '15 at 8:43
4
If thou art indexeth beyond ye ole end of the array, why are you expecting a segfault? It's undefined behavior, anything can happen. By the way, some non-conforming implementations (I know of Apple doing this) have a 3-argumentmain()
like this:int main(int argc, char *argv, char *envp)
where the 3rd argument is an array of environment variables. They might happen to come after argv on the stack or something.
– The Paramagnetic Croissant
Jun 25 '15 at 8:44
Anything can happen, anything!? Like Cthulhu leaping out of a black hole, or my bank account filling up with millions of worthless US Confederate dollars? Hell, we should try this more often just to see what happens.
– Paul
Jun 25 '15 at 8:48
@Paul Don't forget the nasal demons!
– Some programmer dude
Jun 25 '15 at 8:50
1
@SouravGhosh I would love to!
– The Paramagnetic Croissant
Jun 25 '15 at 9:21
|
show 2 more comments
1
If you pass only 3 arguments tomain
but read upto 20 then you have undefined behaviour.
– P.P.
Jun 25 '15 at 8:43
4
If thou art indexeth beyond ye ole end of the array, why are you expecting a segfault? It's undefined behavior, anything can happen. By the way, some non-conforming implementations (I know of Apple doing this) have a 3-argumentmain()
like this:int main(int argc, char *argv, char *envp)
where the 3rd argument is an array of environment variables. They might happen to come after argv on the stack or something.
– The Paramagnetic Croissant
Jun 25 '15 at 8:44
Anything can happen, anything!? Like Cthulhu leaping out of a black hole, or my bank account filling up with millions of worthless US Confederate dollars? Hell, we should try this more often just to see what happens.
– Paul
Jun 25 '15 at 8:48
@Paul Don't forget the nasal demons!
– Some programmer dude
Jun 25 '15 at 8:50
1
@SouravGhosh I would love to!
– The Paramagnetic Croissant
Jun 25 '15 at 9:21
1
1
If you pass only 3 arguments to
main
but read upto 20 then you have undefined behaviour.– P.P.
Jun 25 '15 at 8:43
If you pass only 3 arguments to
main
but read upto 20 then you have undefined behaviour.– P.P.
Jun 25 '15 at 8:43
4
4
If thou art indexeth beyond ye ole end of the array, why are you expecting a segfault? It's undefined behavior, anything can happen. By the way, some non-conforming implementations (I know of Apple doing this) have a 3-argument
main()
like this: int main(int argc, char *argv, char *envp)
where the 3rd argument is an array of environment variables. They might happen to come after argv on the stack or something.– The Paramagnetic Croissant
Jun 25 '15 at 8:44
If thou art indexeth beyond ye ole end of the array, why are you expecting a segfault? It's undefined behavior, anything can happen. By the way, some non-conforming implementations (I know of Apple doing this) have a 3-argument
main()
like this: int main(int argc, char *argv, char *envp)
where the 3rd argument is an array of environment variables. They might happen to come after argv on the stack or something.– The Paramagnetic Croissant
Jun 25 '15 at 8:44
Anything can happen, anything!? Like Cthulhu leaping out of a black hole, or my bank account filling up with millions of worthless US Confederate dollars? Hell, we should try this more often just to see what happens.
– Paul
Jun 25 '15 at 8:48
Anything can happen, anything!? Like Cthulhu leaping out of a black hole, or my bank account filling up with millions of worthless US Confederate dollars? Hell, we should try this more often just to see what happens.
– Paul
Jun 25 '15 at 8:48
@Paul Don't forget the nasal demons!
– Some programmer dude
Jun 25 '15 at 8:50
@Paul Don't forget the nasal demons!
– Some programmer dude
Jun 25 '15 at 8:50
1
1
@SouravGhosh I would love to!
– The Paramagnetic Croissant
Jun 25 '15 at 9:21
@SouravGhosh I would love to!
– The Paramagnetic Croissant
Jun 25 '15 at 9:21
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
8
down vote
accepted
Going outside the limits of any array leads to undefined behavior. What happens in reality, is that many UNIX-like systems (like Linux) actually have a third argument to the main
function, an array of string for the environment variable. So the complete prototype of main
on such systems is
int main(int argc, char *argv, char *envp)
What you do when you go out of bounds of the argv
array is that you cross over into the environ
array.
It should be noted that this isn't actually in any standard, but it's there for backwards compatibility with old UNIX systems where this was common.
It's also mentioned in this reference, and also documented in the GNU libc manual.
Ok got it thanks
– Raman
Jun 25 '15 at 8:48
1
The GNU libc manual mentions this here
– Paul
Jun 25 '15 at 8:58
@Paul Thanks, added a link to it, at the same time you did :)
– Some programmer dude
Jun 25 '15 at 9:01
add a comment |
up vote
0
down vote
There are three ways you can define entry point "main" for your program.
int main () // POSIX.1 style no command-line input
int main (int argc, char *argv) // POSIX.1 style command line inputs
int main (int argc, char *argv, char *envp) // UNIX-specific - env. variable access pointer
Program environment pointer is accessible in UNIX programs. POSIX.1 does not allow this three-argument form, so to be portable it is best to write main to take two arguments, and use the value of environ.
Now coming to the segmentation fault error occurring in the program, it is just because your program tried to access outside the boundaries of third env. pointer.
The C language standard defines only two signatures for main:int main(void)
andint main (int argc, char *argv)
. That has nothing particularly to do with POSIX (which for most intents and purposes is UNIX). The standard also allows implementations to accept arbitrary other signatures, and there are many more than three that are accepted by some implementation somewhere. In any event, none of this seems responsive to the question, which asks why a segmentation fault did not occur in the OP's specific case.
– John Bollinger
Nov 9 at 23:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31045329%2fargv-prints-out-environment-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
Going outside the limits of any array leads to undefined behavior. What happens in reality, is that many UNIX-like systems (like Linux) actually have a third argument to the main
function, an array of string for the environment variable. So the complete prototype of main
on such systems is
int main(int argc, char *argv, char *envp)
What you do when you go out of bounds of the argv
array is that you cross over into the environ
array.
It should be noted that this isn't actually in any standard, but it's there for backwards compatibility with old UNIX systems where this was common.
It's also mentioned in this reference, and also documented in the GNU libc manual.
Ok got it thanks
– Raman
Jun 25 '15 at 8:48
1
The GNU libc manual mentions this here
– Paul
Jun 25 '15 at 8:58
@Paul Thanks, added a link to it, at the same time you did :)
– Some programmer dude
Jun 25 '15 at 9:01
add a comment |
up vote
8
down vote
accepted
Going outside the limits of any array leads to undefined behavior. What happens in reality, is that many UNIX-like systems (like Linux) actually have a third argument to the main
function, an array of string for the environment variable. So the complete prototype of main
on such systems is
int main(int argc, char *argv, char *envp)
What you do when you go out of bounds of the argv
array is that you cross over into the environ
array.
It should be noted that this isn't actually in any standard, but it's there for backwards compatibility with old UNIX systems where this was common.
It's also mentioned in this reference, and also documented in the GNU libc manual.
Ok got it thanks
– Raman
Jun 25 '15 at 8:48
1
The GNU libc manual mentions this here
– Paul
Jun 25 '15 at 8:58
@Paul Thanks, added a link to it, at the same time you did :)
– Some programmer dude
Jun 25 '15 at 9:01
add a comment |
up vote
8
down vote
accepted
up vote
8
down vote
accepted
Going outside the limits of any array leads to undefined behavior. What happens in reality, is that many UNIX-like systems (like Linux) actually have a third argument to the main
function, an array of string for the environment variable. So the complete prototype of main
on such systems is
int main(int argc, char *argv, char *envp)
What you do when you go out of bounds of the argv
array is that you cross over into the environ
array.
It should be noted that this isn't actually in any standard, but it's there for backwards compatibility with old UNIX systems where this was common.
It's also mentioned in this reference, and also documented in the GNU libc manual.
Going outside the limits of any array leads to undefined behavior. What happens in reality, is that many UNIX-like systems (like Linux) actually have a third argument to the main
function, an array of string for the environment variable. So the complete prototype of main
on such systems is
int main(int argc, char *argv, char *envp)
What you do when you go out of bounds of the argv
array is that you cross over into the environ
array.
It should be noted that this isn't actually in any standard, but it's there for backwards compatibility with old UNIX systems where this was common.
It's also mentioned in this reference, and also documented in the GNU libc manual.
edited Jun 25 '15 at 8:59
answered Jun 25 '15 at 8:45
Some programmer dude
293k24244403
293k24244403
Ok got it thanks
– Raman
Jun 25 '15 at 8:48
1
The GNU libc manual mentions this here
– Paul
Jun 25 '15 at 8:58
@Paul Thanks, added a link to it, at the same time you did :)
– Some programmer dude
Jun 25 '15 at 9:01
add a comment |
Ok got it thanks
– Raman
Jun 25 '15 at 8:48
1
The GNU libc manual mentions this here
– Paul
Jun 25 '15 at 8:58
@Paul Thanks, added a link to it, at the same time you did :)
– Some programmer dude
Jun 25 '15 at 9:01
Ok got it thanks
– Raman
Jun 25 '15 at 8:48
Ok got it thanks
– Raman
Jun 25 '15 at 8:48
1
1
The GNU libc manual mentions this here
– Paul
Jun 25 '15 at 8:58
The GNU libc manual mentions this here
– Paul
Jun 25 '15 at 8:58
@Paul Thanks, added a link to it, at the same time you did :)
– Some programmer dude
Jun 25 '15 at 9:01
@Paul Thanks, added a link to it, at the same time you did :)
– Some programmer dude
Jun 25 '15 at 9:01
add a comment |
up vote
0
down vote
There are three ways you can define entry point "main" for your program.
int main () // POSIX.1 style no command-line input
int main (int argc, char *argv) // POSIX.1 style command line inputs
int main (int argc, char *argv, char *envp) // UNIX-specific - env. variable access pointer
Program environment pointer is accessible in UNIX programs. POSIX.1 does not allow this three-argument form, so to be portable it is best to write main to take two arguments, and use the value of environ.
Now coming to the segmentation fault error occurring in the program, it is just because your program tried to access outside the boundaries of third env. pointer.
The C language standard defines only two signatures for main:int main(void)
andint main (int argc, char *argv)
. That has nothing particularly to do with POSIX (which for most intents and purposes is UNIX). The standard also allows implementations to accept arbitrary other signatures, and there are many more than three that are accepted by some implementation somewhere. In any event, none of this seems responsive to the question, which asks why a segmentation fault did not occur in the OP's specific case.
– John Bollinger
Nov 9 at 23:11
add a comment |
up vote
0
down vote
There are three ways you can define entry point "main" for your program.
int main () // POSIX.1 style no command-line input
int main (int argc, char *argv) // POSIX.1 style command line inputs
int main (int argc, char *argv, char *envp) // UNIX-specific - env. variable access pointer
Program environment pointer is accessible in UNIX programs. POSIX.1 does not allow this three-argument form, so to be portable it is best to write main to take two arguments, and use the value of environ.
Now coming to the segmentation fault error occurring in the program, it is just because your program tried to access outside the boundaries of third env. pointer.
The C language standard defines only two signatures for main:int main(void)
andint main (int argc, char *argv)
. That has nothing particularly to do with POSIX (which for most intents and purposes is UNIX). The standard also allows implementations to accept arbitrary other signatures, and there are many more than three that are accepted by some implementation somewhere. In any event, none of this seems responsive to the question, which asks why a segmentation fault did not occur in the OP's specific case.
– John Bollinger
Nov 9 at 23:11
add a comment |
up vote
0
down vote
up vote
0
down vote
There are three ways you can define entry point "main" for your program.
int main () // POSIX.1 style no command-line input
int main (int argc, char *argv) // POSIX.1 style command line inputs
int main (int argc, char *argv, char *envp) // UNIX-specific - env. variable access pointer
Program environment pointer is accessible in UNIX programs. POSIX.1 does not allow this three-argument form, so to be portable it is best to write main to take two arguments, and use the value of environ.
Now coming to the segmentation fault error occurring in the program, it is just because your program tried to access outside the boundaries of third env. pointer.
There are three ways you can define entry point "main" for your program.
int main () // POSIX.1 style no command-line input
int main (int argc, char *argv) // POSIX.1 style command line inputs
int main (int argc, char *argv, char *envp) // UNIX-specific - env. variable access pointer
Program environment pointer is accessible in UNIX programs. POSIX.1 does not allow this three-argument form, so to be portable it is best to write main to take two arguments, and use the value of environ.
Now coming to the segmentation fault error occurring in the program, it is just because your program tried to access outside the boundaries of third env. pointer.
answered Nov 9 at 22:54
Market Queue
12
12
The C language standard defines only two signatures for main:int main(void)
andint main (int argc, char *argv)
. That has nothing particularly to do with POSIX (which for most intents and purposes is UNIX). The standard also allows implementations to accept arbitrary other signatures, and there are many more than three that are accepted by some implementation somewhere. In any event, none of this seems responsive to the question, which asks why a segmentation fault did not occur in the OP's specific case.
– John Bollinger
Nov 9 at 23:11
add a comment |
The C language standard defines only two signatures for main:int main(void)
andint main (int argc, char *argv)
. That has nothing particularly to do with POSIX (which for most intents and purposes is UNIX). The standard also allows implementations to accept arbitrary other signatures, and there are many more than three that are accepted by some implementation somewhere. In any event, none of this seems responsive to the question, which asks why a segmentation fault did not occur in the OP's specific case.
– John Bollinger
Nov 9 at 23:11
The C language standard defines only two signatures for main:
int main(void)
and int main (int argc, char *argv)
. That has nothing particularly to do with POSIX (which for most intents and purposes is UNIX). The standard also allows implementations to accept arbitrary other signatures, and there are many more than three that are accepted by some implementation somewhere. In any event, none of this seems responsive to the question, which asks why a segmentation fault did not occur in the OP's specific case.– John Bollinger
Nov 9 at 23:11
The C language standard defines only two signatures for main:
int main(void)
and int main (int argc, char *argv)
. That has nothing particularly to do with POSIX (which for most intents and purposes is UNIX). The standard also allows implementations to accept arbitrary other signatures, and there are many more than three that are accepted by some implementation somewhere. In any event, none of this seems responsive to the question, which asks why a segmentation fault did not occur in the OP's specific case.– John Bollinger
Nov 9 at 23:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31045329%2fargv-prints-out-environment-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If you pass only 3 arguments to
main
but read upto 20 then you have undefined behaviour.– P.P.
Jun 25 '15 at 8:43
4
If thou art indexeth beyond ye ole end of the array, why are you expecting a segfault? It's undefined behavior, anything can happen. By the way, some non-conforming implementations (I know of Apple doing this) have a 3-argument
main()
like this:int main(int argc, char *argv, char *envp)
where the 3rd argument is an array of environment variables. They might happen to come after argv on the stack or something.– The Paramagnetic Croissant
Jun 25 '15 at 8:44
Anything can happen, anything!? Like Cthulhu leaping out of a black hole, or my bank account filling up with millions of worthless US Confederate dollars? Hell, we should try this more often just to see what happens.
– Paul
Jun 25 '15 at 8:48
@Paul Don't forget the nasal demons!
– Some programmer dude
Jun 25 '15 at 8:50
1
@SouravGhosh I would love to!
– The Paramagnetic Croissant
Jun 25 '15 at 9:21