本文介绍了sbrk()如何在C ++中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里可以阅读一些细节 sbrk()



/ p>

在什么情况下,我想使用 sbrk(),而不是繁琐的 malloc ) new()



btw, sbrk()

解决方案

查看。



调用基本上询问操作系统通过将先前的断点值增加一定量来为应用程序分配一些更多的存储器。这个数量(第一个参数)是你的应用程序获得的额外内存量。



最基本的malloc实现建立在sbrk系统调用上,分裂和跟踪。 函数通常被认为是更好的选择(这就是为什么mallocs像dlmalloc支持#

对于如何工作,在其最简单的级别的sbrk可能看起来像这样:

  uintptr_t current_break; //你的应用程序的一些全局变量。 
//这可能会被进程的操作系统正确跟踪
void * sbrk(intptr_t incr)
{
uintptr_t old_break = current_break;
current_break + = incr;
return(void *)old_break;
}

现代操作系统会做得更多,例如将地图页映射到地址空间并为每个分配的内存块添加跟踪信息。


Where can I read about sbrk() in some detail?

How does it exactly work?

In what situations would I want to use sbrk() instead of the cumbersome malloc() and new()?

btw, what is the expansion for sbrk()?

解决方案

Have a look at the specification for brk/sbrk.

The call basically asks the OS to allocate some more memory for the application by incrementing the previous "break value" by a certain amount. This amount (the first parameter) is the amount of extra memory your application then gets.

Most rudimentary malloc implementations build upon the sbrk system call to get blocks of memory that they split up and track. The mmap function is generally accepted as a better choice (which is why mallocs like dlmalloc support both with an #ifdef).

As for "how it works", an sbrk at its most simplest level could look something like this:

uintptr_t current_break; // Some global variable for your application.
                         // This would probably be properly tracked by the OS for the process
void *sbrk(intptr_t incr)
{
    uintptr_t old_break = current_break;
    current_break += incr;
    return (void*) old_break;
}

Modern operating systems would do far more, such as map pages into the address space and add tracking information for each block of memory allocated.

这篇关于sbrk()如何在C ++中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 00:48