C 高级编程 1

Posted zengkefu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C 高级编程 1相关的知识,希望对你有一定的参考价值。


         
内存管理层次:

   硬件层次:
           内存结构管理
   内核算层次:
           内存映射
           堆扩展
   数据结构层次:
           智能指针:
           stl :在多线程,共享内存有问题
                 SGI公司实现了STL ,开发了OPENGL库          
   语言层次:C:malloc
            c++:new
            
   
   
   malloc.c
   #include <stdio.h>   
   #include <stdlib.h>      
   main()
   {
       int *p1=malloc(4);
       int *p2=malloc(4);
       int *p3=malloc(4);
       int *p4=malloc(4);
       int *p5=malloc(4);
       printf("%p\\n",p1);
       printf("%p\\n",p2);
       printf("%p\\n",p3);
       printf("%p\\n",p4);
       printf("%p\\n",p5);
            
   }
  
  
gcc  malloc.c -o main
[root@monitor ~]# ./main
0x1e95010
0x1e95030
0x1e95050
0x1e95070
0x1e95090





   malloc.cpp
   g++ malloc.cpp -o ./main
   
   #include <stdio.h>   
   #include <stdlib.h>      
   main()
   {
       int *p1=new int(4);
       int *p2=new int(4);
       int *p3=new int(4);
       int *p4=new int(4);
       int *p5=new int(4);
       printf("%p\\n",p1);
       printf("%p\\n",p2);
       printf("%p\\n",p3);
       printf("%p\\n",p4);
       printf("%p\\n",p5);
            
   }

[root@monitor ~]# ./main
0x10c4010
0x10c4030
0x10c4050
0x10c4070
0x10c4090



malloc

2.linux 对内存的结构描述
  /proc/${pid}      //存放进程运行的所有信息
  
  任何程序内存空间分成4个基本部分
  1.代码区
  2.全局栈区
  3.堆
  4.局部栈区
  
   虚拟内存技术使得每个进程都可以独占整个内存空间,地址从零开始,直到内存上限。 每个进程都将这部分空间(从低地址到高地址)分为六个部分:

    TEXT段:整个程序的代码,以及所有的常量。这部分内存是是固定大小的,只读的。
    DATA段,又称GVAR:初始化为非零值的全局变量。
    BSS段:初始化为0或未初始化的全局变量和静态变量。
    HEAP(堆):动态内存区域,使用malloc或new申请的内存。
    未使用的内存。
    STACK(栈):局部变量、参数、返回值都存在这里,函数调用开始会参数入栈、局部变量入栈;调用结束依次出栈

https://sploitfun.wordpress.com/2015/02/11/syscalls-used-by-malloc/
https://sploitfun.wordpress.com/2015/02/10/understanding-glibc-malloc/comment-page-1/
http://www.evil0x.com/posts/23220.html

3.理解程序的变量与程序内存空间的关系 HEAP与STACK 是一种数据结构 --------------------------------------------------------- test.c main() { while(1); } [root@monitor ~]# gcc test.c -o main [root@monitor ~]# ./main [root@monitor proc]# ps -ef|grep main root 6604 6487 99 08:01 pts/0 00:02:42 ./main root 6626 6609 0 08:03 pts/1 00:00:00 grep main 在/proc/目录下有一个一个程序目录是PID=6604 对应一个目录 /proc/6604 [root@monitor proc]# cd /proc/6604 [root@monitor 6604]# ll total 0 dr-xr-xr-x 2 root root 0 May 18 08:07 attr -rw-r--r-- 1 root root 0 May 18 08:07 autogroup -r-------- 1 root root 0 May 18 08:07 auxv -r--r--r-- 1 root root 0 May 18 08:07 cgroup --w------- 1 root root 0 May 18 08:07 clear_refs -r--r--r-- 1 root root 0 May 18 08:03 cmdline -rw-r--r-- 1 root root 0 May 18 08:07 comm -rw-r--r-- 1 root root 0 May 18 08:07 coredump_filter -r--r--r-- 1 root root 0 May 18 08:07 cpuset lrwxrwxrwx 1 root root 0 May 18 08:07 cwd -> /root -r-------- 1 root root 0 May 18 08:07 environ lrwxrwxrwx 1 root root 0 May 18 08:01 exe -> /root/main dr-x------ 2 root root 0 May 18 08:03 fd dr-x------ 2 root root 0 May 18 08:07 fdinfo -r-------- 1 root root 0 May 18 08:07 io -rw------- 1 root root 0 May 18 08:07 limits -rw-r--r-- 1 root root 0 May 18 08:07 loginuid -r--r--r-- 1 root root 0 May 18 08:07 maps -rw------- 1 root root 0 May 18 08:07 mem -r--r--r-- 1 root root 0 May 18 08:07 mountinfo -r--r--r-- 1 root root 0 May 18 08:07 mounts -r-------- 1 root root 0 May 18 08:07 mountstats dr-xr-xr-x 4 root root 0 May 18 08:07 net dr-x--x--x 2 root root 0 May 18 08:07 ns -r--r--r-- 1 root root 0 May 18 08:07 numa_maps -rw-r--r-- 1 root root 0 May 18 08:07 oom_adj -r--r--r-- 1 root root 0 May 18 08:07 oom_score -rw-r--r-- 1 root root 0 May 18 08:07 oom_score_adj -r--r--r-- 1 root root 0 May 18 08:07 pagemap -r--r--r-- 1 root root 0 May 18 08:07 personality lrwxrwxrwx 1 root root 0 May 18 08:07 root -> / -rw-r--r-- 1 root root 0 May 18 08:07 sched -r--r--r-- 1 root root 0 May 18 08:07 schedstat -r--r--r-- 1 root root 0 May 18 08:07 sessionid -r--r--r-- 1 root root 0 May 18 08:07 smaps -r--r--r-- 1 root root 0 May 18 08:07 stack -r--r--r-- 1 root root 0 May 18 08:02 stat -r--r--r-- 1 root root 0 May 18 08:07 statm -r--r--r-- 1 root root 0 May 18 08:03 status -r--r--r-- 1 root root 0 May 18 08:07 syscall dr-xr-xr-x 3 root root 0 May 18 08:07 task -r--r--r-- 1 root root 0 May 18 08:07 wchan [root@monitor 6604]# ls -l exe lrwxrwxrwx 1 root root 0 May 18 08:01 exe -> /root/main [root@monitor 6604]# ls -l cwd lrwxrwxrwx 1 root root 0 May 18 08:07 cwd -> /root [root@monitor 6604]# cat maps //code x 00400000-00401000 r-xp 00000000 ca:01 1065168 /root/main //全局栈区(全局变量) 00600000-00601000 rw-p 00000000 ca:01 1065168 /root/main 3c4e400000-3c4e420000 r-xp 00000000 ca:01 262151 /lib64/ld-2.12.so 3c4e61f000-3c4e620000 r--p 0001f000 ca:01 262151 /lib64/ld-2.12.so 3c4e620000-3c4e621000 rw-p 00020000 ca:01 262151 /lib64/ld-2.12.so 3c4e621000-3c4e622000 rw-p 00000000 00:00 0 3c4ec00000-3c4ed8a000 r-xp 00000000 ca:01 262157 /lib64/libc-2.12.so 3c4ed8a000-3c4ef8a000 ---p 0018a000 ca:01 262157 /lib64/libc-2.12.so 3c4ef8a000-3c4ef8e000 r--p 0018a000 ca:01 262157 /lib64/libc-2.12.so 3c4ef8e000-3c4ef8f000 rw-p 0018e000 ca:01 262157 /lib64/libc-2.12.so 3c4ef8f000-3c4ef94000 rw-p 00000000 00:00 0 7faf76e78000-7faf76e7b000 rw-p 00000000 00:00 0 7faf76e82000-7faf76e83000 rw-p 00000000 00:00 0 //stack(局部栈) 7fff8dd1d000-7fff8dd32000 rw-p 00000000 00:00 0 [stack] 7fff8dd4d000-7fff8dd4e000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] [root@monitor ~]# ldd main linux-vdso.so.1 => (0x00007fffb1dbd000) libc.so.6 => /lib64/libc.so.6 (0x0000003c4ec00000) //标准的执行程序,把程序拷到代码区,并执行 /lib64/ld-linux-x86-64.so.2 (0x0000003c4e400000) [root@monitor ~]# /lib64/ld-linux-x86-64.so.2 ./main ---------------------------------------------------- test.c #include <unistd.h> #include <stdio.h> main() { printf("%d\\n",getpid()); //取得PID while(1); } X86_64 的 Redhat / Centos / Scientific 下面,若要编译、运行32位程序,需要安装以下包: yum install libgcc.i686 yum install glibc-static.i686 yum install glibc-devel.i686 http://www.cnblogs.com/hummersofdie/p/3953043.html [root@monitor ~]# gcc test.c -m32 -o main //编绎为32位程序 [root@monitor ~]# ./main 6747 [root@monitor 6747]# cat maps 001e0000-00370000 r-xp 00000000 ca:01 396356 /lib/libc-2.12.so 00370000-00371000 ---p 00190000 ca:01 396356 /lib/libc-2.12.so 00371000-00373000 r--p 00190000 ca:01 396356 /lib/libc-2.12.so 00373000-00374000 rw-p 00192000 ca:01 396356 /lib/libc-2.12.so 00374000-00377000 rw-p 00000000 00:00 0 0086a000-0086b000 r-xp 00000000 00:00 0 [vdso] 00bc0000-00bde000 r-xp 00000000 ca:01 396343 /lib/ld-2.12.so 00bde000-00bdf000 r--p 0001d000 ca:01 396343 /lib/ld-2.12.so 00bdf000-00be0000 rw-p 0001e000 ca:01 396343 /lib/ld-2.12.so //code 08048000-08049000 r-xp 00000000 ca:01 1054108 /root/main //global stack 08049000-0804a000 rw-p 00000000 ca:01 1054108 /root/main //heap f77b2000-f77b3000 rw-p 00000000 00:00 0 f77ba000-f77bc000 rw-p 00000000 00:00 0 //stack ffa81000-ffa96000 rw-p 00000000 00:00 0 [stack] [root@monitor 6747]# cd /root [root@monitor ~]# ldd main linux-gate.so.1 => (0x00296000) libc.so.6 => /lib/libc.so.6 (0x00297000) /lib/ld-linux.so.2 (0x00744000) --------------------------------------------------------------- test.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> int add(int a,int b) { return a+b; } int a1=1; static int a2=2; const int a3=3; //code // const 都放到了代码区 // static 放到全局区 main() { int b1=4; static b2=5; //global stack const b3=6; //code int *p1=malloc(4); printf("a1:%p\\n",&a1); printf("a2:%p\\n",&a2); printf("a3:%p\\n",&a3); printf("b1:%p\\n",&b1); printf("b2:%p\\n",&b2); printf("b3:%p\\n",&b3); printf("p1:%p\\n",p1); printf("main:%p\\n",main); printf("add:%p\\n",add); printf("pid:%d\\n",getpid()); while(1); } [root@monitor ~]# ./test a1:0x80497e8 a2:0x80497ec a3:0x80485f4 b1:0xffde8198 b2:0x80497f0 b3:0xffde8194 p1:0x81aa008 //P1分配的内存在HEAP P1变量在STACK main:0x8048432 add:0x8048424 pid:6825 [root@monitor 6810]# cd /proc [root@monitor proc]# cd 6825 [root@monitor 6825]# cat maps 001e1000-00371000 r-xp 00000000 ca:01 396356 /lib/libc-2.12.so 00371000-00372000 ---p 00190000 ca:01 396356 /lib/libc-2.12.so 00372000-00374000 r--p 00190000 ca:01 396356 /lib/libc-2.12.so 00374000-00375000 rw-p 00192000 ca:01 396356 /lib/libc-2.12.so 00375000-00378000 rw-p 00000000 00:00 0 005cf000-005ed000 r-xp 00000000 ca:01 396343 /lib/ld-2.12.so 005ed000-005ee000 r--p 0001d000 ca:01 396343 /lib/ld-2.12.so 005ee000-005ef000 rw-p 0001e000 ca:01 396343 /lib/ld-2.12.so 00a03000-00a04000 r-xp 00000000 00:00 0 [vdso] //1G //3G //code main ,add,a3 08048000-08049000 r-xp 00000000 ca:01 1065171 /root/test //global stack a1,a2,b2 08049000-0804a000 rw-p 00000000 ca:01 1065171 /root/test //heap p1 081aa000-081cb000 rw-p 00000000 00:00 0 [heap] f770b000-f770c000 rw-p 00000000 00:00 0 f7713000-f7715000 rw-p 00000000 00:00 0 //stack b1,b3 ffdd4000-ffde9000 rw-p 00000000 00:00 0 [stack] ------------------------------------------------------------------------------------------------------------------------------- test.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> main() { int a1=10; int a2-20; int a3=30; int *p1=malloc(4); int *p2=malloc(4); int *p3=malloc(4); printf("%p\\n",a1); printf("%p\\n",a2); printf("%p\\n",a3); printf("%p\\n",p1); printf("%p\\n",p2); printf("%p\\n",p3); printf("%d\\n",getpid()); while(1); } [root@monitor ~]# gcc test.c -m32 -o test [root@monitor ~]# ./test 0xa 0x14 0x1e 0x8120008 0x8120018 0x8120028 7002 [root@monitor 7002]# cat maps 003a6000-003c4000 r-xp 00000000 ca:01 396343 /lib/ld-2.12.so 003c4000-003c5000 r--p 0001d000 ca:01 396343 /lib/ld-2.12.so 003c5000-003c6000 rw-p 0001e000 ca:01 396343 /lib/ld-2.12.so 0045f000-005ef000 r-xp 00000000 ca:01 396356 /lib/libc-2.12.so 005ef000-005f0000 ---p 00190000 ca:01 396356 /lib/libc-2.12.so 005f0000-005f2000 r--p 00190000 ca:01 396356 /lib/libc-2.12.so 005f2000-005f3000 rw-p 00192000 ca:01 396356 /lib/libc-2.12.so 005f3000-005f6000 rw-p 00000000 00:00 0 0063d000-0063e000 r-xp 00000000 00:00 0 [vdso] 08048000-08049000 r-xp 00000000 ca:01 1065171 /root/test 08049000-0804a000 rw-p 00000000 ca:01 1065171 /root/test 08120000-08141000 rw-p 00000000 00:00 0 [heap] f774d000-f774e000 rw-p 00000000 00:00 0 f7755000-f7757000 rw-p 00000000 00:00 0 ff834000-ff849000 rw-p 00000000 00:00 0 [stack] [root@monitor 7002]# ------------------------------------------------------------------------------------------ #include <stdio.h> #include <stdlib.h> #include <unistd.h> main() { int *p1=malloc(4); int *p2=malloc(4); int *p3=malloc(4); *p1=1; *(p1+1)=2; *(p1+2)=3; *(p1+3)=4; *(p1+4)=5; *(p1+5)=6; *(p1+6)=7; *(p1+7)=8; printf("%d\\n",*p2); printf("%d\\n",getpid()); while(1); } [root@monitor ~]# gcc test.c -m32 -o test [root@monitor ~]# ./test 5 7127 http://blog.jobbole.com/91887/ http://blog.csdn.net/ordeder/article/details/41654509 --------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <unistd.h> main() { printf("%d\\n",getpid()); int *p1=C 高级编程 1

Atom编辑器入门到精通 Atom使用进阶

unix高级编程中的一个头文件 apue.h 与一个差错文件error.c 的内容

WPF高级编程的目录

Linux C高级编程——网络编程

使用硬件通信进行高级编程