内存管理层次:硬件层次:内存结构管理内核算层次:内存映射堆扩展数据结构层次:智能指针:stl :在多线程,共享内存有问题SGI公司实现了STL ,开发了OPENGL库 语言层次:C:mallocc++:newmalloc.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 0x1e95090malloc.cppg++ 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 0x10c4090malloc2.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.cmain() {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/main3c4e400000-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 mainlinux-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()); //取得PIDwhile(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 maps001e0000-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 mainlinux-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 stackconst 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/test08120000-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 7127http://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=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);free(p1);} ~ ~ "test.c" 26L, 337C written [root@monitor ~]# gcc test.c -m32 -o test [root@monitor ~]# ./test 5 *** glibc detected *** ./test: free(): invalid next size (fast): 0x08f25008 *** ======= Backtrace: ========= /lib/libc.so.6(+0x70c81)[0x64bc81] /lib/libc.so.6(+0x73601)[0x64e601] ./test[0x80484e5] /lib/libc.so.6(__libc_start_main+0xe6)[0x5f1d36] ./test[0x8048391] ======= Memory map: ======== 00237000-00255000 r-xp 00000000 ca:01 396343 /lib/ld-2.12.so 00255000-00256000 r--p 0001d000 ca:01 396343 /lib/ld-2.12.so 00256000-00257000 rw-p 0001e000 ca:01 396343 /lib/ld-2.12.so 005db000-0076b000 r-xp 00000000 ca:01 396356 /lib/libc-2.12.so 0076b000-0076c000 ---p 00190000 ca:01 396356 /lib/libc-2.12.so 0076c000-0076e000 r--p 00190000 ca:01 396356 /lib/libc-2.12.so 0076e000-0076f000 rw-p 00192000 ca:01 396356 /lib/libc-2.12.so 0076f000-00772000 rw-p 00000000 00:00 0 007ef000-0080c000 r-xp 00000000 ca:01 393234 /lib/libgcc_s-4.4.7-20120601.so.1 0080c000-0080d000 rw-p 0001d000 ca:01 393234 /lib/libgcc_s-4.4.7-20120601.so.1 00cdb000-00cdc000 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 08f25000-08f46000 rw-p 00000000 00:00 0 [heap] f7600000-f7621000 rw-p 00000000 00:00 0 f7621000-f7700000 ---p 00000000 00:00 0 f7771000-f7772000 rw-p 00000000 00:00 0 f7778000-f777b000 rw-p 00000000 00:00 0 ffa41000-ffa56000 rw-p 00000000 00:00 0 [stack] Aborted理解malloc的工作原理:malloc使用一个数据结构(链表)来维护分配的空间。链表的构成: 分配的空间、上一个空间的地址、下一个空间的地址、以及本空间的信息等。 对malloc分配的空间不要越界访问, 因为容易破坏后台的链表维护结构,导致malloc/free/calloc/realloc不正常工作。
C++的new与malloc的关系
小实验:
#include <stdlib.h>
#include <unistd.h>
int main()
{
int *p1 = (int*)malloc(4);
int *p2 = new int;
int *p3 = (int *)malloc(4);
int *p4 = new int;
int *p5 = new int;
int *p6 = new int;
printf("%p\n", p1);
printf("%p\n", p2);
printf("%p\n", p3);
printf("%p\n", p4);
printf("%p\n", p5);
printf("%p\n", p6);
return 0;
}
运行结果:
结论:new的实现使用的是malloc来实现的。
区别:new使用malloc后,还要初始化空间。
基本类型:直接初始化成默认值。
UDT类型:调用指定的构造器
推论:delete也是调用free实现。
区别:delete会调用指定的析构器,然后再调用free()。
new与new[]的区别:new只调用一个构造器初始化。new[]循环对每个区域调用构造器。
Stu *p=new Stu[30] :创建30个数组对象,每个对象都调用构造器
delete与delete[]的区别:delete只调用一次析构函数,而delete则把数组中的每个对象的析构函数都调用一遍。
eg:Stu *p=new Stu[30]
delete p; 指对P[0]的析构函数调用
delete[] p ;delete则把数组中的每个对象的析构函数都调用一遍,不指定delete[30] p ,而是由系统搞定计算,[],只表示每个区域的对象调用析构函数
都能把p指定的空间释放空间
----------------------------------------------
C 函数 对应 C++函数
malloc new
realloc new() //定位分配
calloc new[]
free delete
-------------------------------------------------------------
http://blog.csdn.net/shuaishuai80/article/details/6140979
定位分配:
EG:在指定的位置a分配空间
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <new>
int main()
{
char a[20];
int *p=new(a) int;
return 0;
}