exercise 2: probing argv[] and environ[]
Write a program which prints the values of argc, argv[], and environ[]. Also their addresses are printed. Based on the printed information, you can figure out the memory layout of argc, argv[], and environ[].
For example:
$ ./a.out klim is not milk
argc is 5
argv[0]: ./a.out
argv[1]: klim
argv[2]: is
argv[3]: not
argv[4]: milk
environ[0]: CPLUS_INCLUDE_PATH=/usr/lib64/qt/include
environ[1]: NNTPSERVER=news.ncnu.edu.tw
.......
environ[33]: INFOPATH=/usr/info:/usr/share/texmf/info
environ[34]: G_BROKEN_FILENAMES=1
environ[35]: _=./a.out
environ[36]: OLDPWD=/home/klim/course/usp-1092
argc is resided at 0x7ffdddad11fc
argv is resided at 0x7ffdddad11f0
environ is resided at 0x601040
argv[] is resided at 0x7ffdddad12f8
environ[] is resided at 0x7ffdddad1328
value of argv[ 0] is 0x7ffdddad24c3
value of argv[ 1] is 0x7ffdddad24cb
value of argv[ 2] is 0x7ffdddad24d0
value of argv[ 3] is 0x7ffdddad24d3
value of argv[ 4] is 0x7ffdddad24d7
value of argv[ 5] is (nil)
value of env [ 0] is 0x7ffdddad24dc
value of env [ 1] is 0x7ffdddad2505
value of env [ 2] is 0x7ffdddad2521
..........
value of env [35] is 0x7ffdddad2fc4
value of env [36] is 0x7ffdddad2fce
$
If there are too many environment strings to display, you can try to use ``env'' to get a cleaner environment. For example:
$ env -i A=1 hello=ohio ./a.out see you again
argc is 4
argv[0]: ./a.out
argv[1]: see
argv[2]: you
argv[3]: again
environ[0]: A=1
environ[1]: hello=ohio
argc is resided at 0x7fff4b9083dc
argv is resided at 0x7fff4b9083d0
environ is resided at 0x601040
argv[] is resided at 0x7fff4b9084d8
environ[] is resided at 0x7fff4b908500
value of argv[ 0] is 0x7fff4b908fcb
value of argv[ 1] is 0x7fff4b908fd3
value of argv[ 2] is 0x7fff4b908fd7
value of argv[ 3] is 0x7fff4b908fdb
value of argv[ 4] is (nil)
value of env [ 0] is 0x7fff4b908fe1
value of env [ 1] is 0x7fff4b908fe5
$
https://ideone.com/xz3wfv
沒有留言:
張貼留言