打印不同段数据的位置
#include <stdio.h> #include <stdlib.h> #include <unistd.h> extern char** environ; int global1; int global2 = 1; int main() { //1.环境变量 char **env = environ; while(*env){ printf("(%p) (%p) %s\n",env,*env,*env); //%p打印指针位置(所指地址) env++; } printf("\n"); //2.全局变量(未初始化和初始化) printf("%p\n",&global1); printf("%p\n\n",&global2); //3.栈 int local; char *localc = (char*)malloc(10); //指针在栈中,指针内容在堆中 printf("%p\n",&local); printf("%p\n\n",&localc); //4.堆 printf("%p\n\n",localc); return 0; }
我的环境,输出如下: 1 chenximing@chenximing-MS-7823:~$ ./a.out
//首先打印了 环境表地址 环境变量地址 环境变量字符串 2 (0x7ffe4aa6f4c8) (0x7ffe4aa7078a) XDG_SESSION_ID=1 3 (0x7ffe4aa6f4d0) (0x7ffe4aa7079b) TERM=xterm-256color 4 (0x7ffe4aa6f4d8) (0x7ffe4aa707af) SHELL=/bin/bash 5 (0x7ffe4aa6f4e0) (0x7ffe4aa707bf) XDG_SESSION_COOKIE=526d72c400c09a742df8809a58e50d0e-1529375799.783462-1767108743 6 (0x7ffe4aa6f4e8) (0x7ffe4aa70810) SSH_CLIENT=192.168.2.1 53068 22 7 (0x7ffe4aa6f4f0) (0x7ffe4aa70830) SSH_TTY=/dev/pts/25 8 (0x7ffe4aa6f4f8) (0x7ffe4aa70844) USER=chenximing
9 ...
10 (0x7ffe4aa6f508) (0x7ffe4aa70d75) MAIL=/var/mail/chenximing 11 (0x7ffe4aa6f510) (0x7ffe4aa70d8f) PATH=/home/chenximing/bin:/usr/local/go/bin:/usr/lib/jvm/jdk1.8.0_144/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/chenximing/bin 12 (0x7ffe4aa6f518) (0x7ffe4aa70e47) QT_QPA_PLATFORMTHEME=appmenu-qt5 13 (0x7ffe4aa6f520) (0x7ffe4aa70e68) PWD=/home/chenximing 14 (0x7ffe4aa6f528) (0x7ffe4aa70e7d) JAVA_HOME=/usr/lib/jvm/jdk1.8.0_144 15 (0x7ffe4aa6f530) (0x7ffe4aa70ea1) LANG=zh_CN.UTF-8 16 (0x7ffe4aa6f538) (0x7ffe4aa70eb2) SHLVL=1 17 (0x7ffe4aa6f540) (0x7ffe4aa70eba) HOME=/home/chenximing 18 (0x7ffe4aa6f548) (0x7ffe4aa70ed0) GOROOT=/usr/local/go 19 (0x7ffe4aa6f550) (0x7ffe4aa70ee5) LANGUAGE=zh_CN:zh 20 (0x7ffe4aa6f558) (0x7ffe4aa70ef7) LOGNAME=chenximing 21 (0x7ffe4aa6f560) (0x7ffe4aa70f0a) CLASSPATH=.:/usr/lib/jvm/jdk1.8.0_144/lib 22 (0x7ffe4aa6f568) (0x7ffe4aa70f34) SSH_CONNECTION=192.168.2.1 53068 192.168.2.4 22 23 (0x7ffe4aa6f570) (0x7ffe4aa70f64) LC_CTYPE=zh_CN.UTF-8 24 (0x7ffe4aa6f578) (0x7ffe4aa70f79) GOPATH=~/go 25 (0x7ffe4aa6f580) (0x7ffe4aa70f85) LESSOPEN=| /usr/bin/lesspipe %s 26 (0x7ffe4aa6f588) (0x7ffe4aa70fa5) XDG_RUNTIME_DIR=/run/user/1000 27 (0x7ffe4aa6f590) (0x7ffe4aa70fc4) LESSCLOSE=/usr/bin/lesspipe %s %s 28 (0x7ffe4aa6f598) (0x7ffe4aa70fe6) _=./a.out 29 //以下是各字段变量的地址 30 0x60106c 31 0x601050 32 33 0x7ffe4aa6f3bc 34 0x7ffe4aa6f3c0 35 36 0x1725010
画图表示: