[英]Difference between brk() , sbrk() and realloc() functions
我知道函数brk(),sbrk()用于重新分配内存。但它们与realloc()函数有什么不同?。给我编码示例。
brk
and sbrk
are system calls (implemented in the kernel) while malloc
, free
, realloc
are library functions in user space. brk
和sbrk
是系统调用(在内核中实现),而malloc
, free
, realloc
是用户空间中的库函数。 So the malloc
etc functions use brk
and sbrk
internally but provide additional functionality (see man(2) for more details about brk
and man(3) for more details about malloc
.). 因此
malloc
等函数在内部使用brk
和sbrk
但提供了额外的功能(有关malloc
更多详细信息,请参阅man(2)以获取有关brk
和man(3)的更多详细信息。)。
brk
only tells the kernel how much memory your program wants to use, by giving the the kernel a pointer to the largest virtual memory location that your program may use. brk
只通过向内核提供指向程序可能使用的最大虚拟内存位置的指针,告诉内核程序想要使用多少内存。 But you only have exactly one big chunk of memory. 但是你只有一大块内存。
malloc
helps you to subdivide this huge block of memory into smaller parts. malloc
帮助您将这个巨大的内存块细分为更小的部分。
Example code doesn't make much sense here, because brk
and malloc
work on different levels. 示例代码在这里没有多大意义,因为
brk
和malloc
在不同的级别上工作。 But you could think how you would implement a very simple (and non-thread-safe) version of malloc
and free
and where you would use brk
there: 但你可以想一想如何实现一个非常简单(和非线程安全)的
malloc
和free
版本以及你在那里使用brk
地方:
brk
to increase the usable memory we got from the kernel brk
来增加我们从内核获得的可用内存 And as @BasileStarynkevitch remarked in his comment, as an alternative to brk
you could also use mmap
(with fd=-1
and flags=MAP_PRIVATE|MAP_ANONYMOUS
) to reserve a single block of memory backed by the swap file. 正如@BasileStarynkevitch在评论中所说,作为
brk
的替代方法,您还可以使用mmap
(使用fd=-1
和flags=MAP_PRIVATE|MAP_ANONYMOUS
)来保留由交换文件支持的单个内存块。 For details about mmap see man(2) . 有关mmap的详细信息,请参阅man(2) 。
At the OS level (in the Unix model, at least), your program has one big region of memory for program text, initialized and uninitialized data, and the "heap", for dynamically-allocated data. 在操作系统级别(至少在Unix模型中),程序文本,初始化和未初始化数据以及动态分配数据的“堆”都有一个很大的内存区域。 (The stack is separate.) You can adjust the size of that region using
brk
and sbrk
, but you can't rearrange it, and it's always contiguous. (堆栈是独立的。)您可以使用
brk
和sbrk
调整该区域的大小,但不能重新排列它,它总是连续的。 The vast majority of programs that do dynamic memory allocation require something more flexible. 绝大多数进行动态内存分配的程序需要更灵活的东西。
malloc
, free
, and realloc
are C library functions that give you something more flexible. malloc
, free
和realloc
是C库函数,可以为您提供更灵活的功能。 Underneath, they get memory from the OS by calling brk
and/or sbrk
, but then they do extra processing to let you allocate (a) any number of chunks of (b) different sizes and which you can (c) individually return to the pool when you're done with them and incidentally (d) resize. 在下面,他们通过调用
brk
和/或sbrk
从操作系统获取内存,但随后他们做了额外的处理,让你分配(a)任意数量的(b)不同大小的块,你可以(c)单独返回到当你完成它们并且偶然地(d)重新调整大小时。
But when you return memory to the pool with free
, it generally just goes back into the pool that future calls to malloc
by your program will draw from; 但是当你以
free
将内存返回到池中时,它通常只会回到池中,以后您的程序将来自malloc
的调用将从中抽取; memory is generally not given back to the OS. 内存通常不会返回给操作系统。
(Sorry for not providing any example code; I don't have time for that just now.) (很抱歉没有提供任何示例代码;我刚才没有时间。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.