关于sbrk()和malloc() [英] About sbrk() and malloc()

查看:187
本文介绍了关于sbrk()和malloc()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经彻底阅读了有关sbrk()的linux手册:

I've read the linux manual about sbrk() thoroughly:

sbrk()更改程序中断的位置,该位置定义结束 流程数据段中的第一个(即,程序中断是第一个 未初始化数据段末尾的位置.

sbrk() changes 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).

我确实知道用户空间内存的组织如下:

And I do know that user space memory's organization is like the following:

问题是: 当我调用sbrk(1)时,为什么会说我在增加堆的大小?如手册所述,我正在更改数据段和bss"的结束位置.因此,增加的应该是数据段的大小bss,对吗?

The problem is: When I call sbrk(1), why does it say I am increasing the size of heap? As the manual says, I am changing the end position of "data segment & bss". So, what increases should be the size of data segment & bss, right?

推荐答案

data和bss段是固定大小的.因此,在这些段结束之后分配给进程的空间不是这些段的一部分;它只是与它们相邻.该空间称为堆空间,用于动态内存分配.

The data and bss segments are a fixed size. The space allocated to the process after the end of those segments is therefore not a part of those segments; it is merely contiguous with them. And that space is called the heap space and is used for dynamic memory allocation.

如果您想将其视为扩展数据/bss段",也可以.它不会对程序的行为,分配的空间或其他任何行为产生任何影响.

If you want to regard it as 'extending the data/bss segment', that's fine too. It won't make any difference to the behaviour of the program, or the space that's allocated, or anything.

Mac OS X上的手册页指示您确实不应该过多地使用它们:

The manual page on Mac OS X indicates you really shouldn't be using them very much:

brk和sbrk函数是虚拟内存管理出现之前的早期遗留的历史问题. brk()函数设置进程的数据段的中断或最低地址(未初始化的数据) )到addr(紧接在bss上方).数据寻址限制在addr和指向堆栈段的最低堆栈指针之间.内存由brk按页面大小分配;如果addr不能被系统页面大小均分,则将其增加到下一个页面边界.

The brk and sbrk functions are historical curiosities left over from earlier days before the advent of virtual memory management. The brk() function sets the break or lowest address of a process's data segment (uninitialized data) to addr (immediately above bss). Data addressing is restricted between addr and the lowest stack pointer to the stack segment. Memory is allocated by brk in page size pieces; if addr is not evenly divisible by the system page size, it is increased to the next page boundary.

sbrk(0)可靠地返回程序中断的当前值(另请参见 end(3)). getrlimit(2)系统调用可用于确定数据段的最大允许大小.无法将中断设置为超出从调用返回到getrlimitrlim_max值,例如etext + rlp->rlim_max(有关etext的定义,请参见 end(3)).

The current value of the program break is reliably returned by sbrk(0) (see also end(3)). The getrlimit(2) system call may be used to determine the maximum permissible size of the data segment; it will not be possible to set the break beyond the rlim_max value returned from a call to getrlimit, e.g. etext + rlp->rlim_max (see end(3) for the definition of etext).

尽管有指针来查看它,但我找不到 end(3)的手册页,这令人有些恼火.即使是 sbrk() 没有链接.

It is mildly exasperating that I can't find a manual page for end(3), despite the pointers to look at it. Even this (slightly old) manual page for sbrk() does not have a link for it.

这篇关于关于sbrk()和malloc()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆