了解ç的malloc和SBRK() [英] Understanding C malloc and sbrk()

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

问题描述

我想了解用C的malloc和SBRK之间的区别,以及它们如何相互关联的。从我了解的malloc和SBRK是pretty同样的事情,但我读的malloc使用SBRK中分配内存。这真是混乱有人可以解释给我吗?

I am trying to understand the difference between malloc and sbrk in C and how they relate to each other. From what I understand malloc and sbrk are pretty much the same thing but I read that malloc uses sbrk in allocating memory. This is really confusing can someone explain it to me?

例如在这个方案确实malloc调用SBRK?如果这样做它只是SBRK它被称为每次打电话,所以在这个例子中10次?

For example in this program does malloc call sbrk? if so does it simply call sbrk each time it gets called, so for this example 10 times?

int main(int argc, char **argv) {
        int i;
        void *start_pos, *finish_pos;
        void *res[10];
        start_pos = sbrk(0);
        for (i = 0; i < 10; i++) {
                res[i] = malloc(10);
        }
        finish_pos = sbrk(0);
        return 0;
}

感谢您,

推荐答案

SBRK 要求从操作系统更多的内存。这是一个pretty低级别的功能,而不是很灵活。

sbrk requests more memory from the operating system. It is a pretty low-level function and not very flexible.

的malloc 使用 SBRK ,但更灵活。一般来说,的malloc 会问 SBRK 为大块的内存,然后施舍那些大块的碎片。因此,大多数来电的malloc 将的的结果调用 SBRK

malloc uses sbrk, but is more flexible. Generally, malloc will ask sbrk for large chunks of memory and then dole out pieces of those large chunks. So most calls to malloc will not result in calls to sbrk.

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

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