brk,sbrk

内核数据结构mm_struct中的成员变量start_code和end_code是进程代码段的起始和终止地址,start_data和 end_data是进程数据段的起始和终止地址,start_stack是进程堆栈段起始地址,start_brk是进程动态内存分配起始地址(堆的起始地址),还有一个 brk(堆的当前最后地址),就是动态内存分配当前的终止地址。

man 2 sbrk的结果:

brk()  and  sbrk()  change  the  location  of  the program break, which
       defines the end of the process's data segment (i.e., the program  break
       is the first location after the end of the uninitialized data segment).
       Increasing the program break has the effect of allocating memory to the
       process; decreasing the break deallocates memory.

看上面的说明让我疑惑,进程堆栈段不是向上生长的吗,为什么增加break能分配内存呢?

堆栈虽然是向上生长的,但是break为进程的数据段终止点,因此增加brk会动态分配内存。声明一个指针变量来指向动态分配的内存才会导致堆栈段向上生长。

posted @ 2013-06-11 14:13  farseeraliens  阅读(146)  评论(0编辑  收藏  举报