堆栈上运行时内存分配 [英] Runtime Memory allocation on stack

查看:114
本文介绍了堆栈上运行时内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有关堆栈区运行时内存的分配和它是如何从不同运行时的内存分配的堆区。

I want to know about runtime memory allocation on stack area and how its different from runtime memory allocation on Heap area.

我知道内存是如何被使用的库函数分配。

I know how memory get allocated by using library function.

#include<alloca.h> void *alloca(size_t size); //(for runtime memory on stack)

#include<stdlib.h> void *malloc(size_t size); //(for run time memory on heap)

我也知道,如果我们使用了alloca功能我们并不需要显式地释放内存,因为它是与栈相关联,它都会自动释放。

I also know that if we are using alloca function we don't need to free that memory explicitly because it is associated with stack, its get freed automatically.

我想知道哪些系统调用与alloc和相关的malloc,他们两个是如何工作的。

I want to know which system calls are associated with alloc and malloc and how they works in both.

推荐答案

在短期,他们通常不使用的系统调用,除非耗尽可用内存。

In short they usually don't use system calls, unless running out of available memory.

该bahavior对于两种不同的,所以我解释不同。

The bahavior is different for either, so I explain differently.

让我们开始说你的程序有1MB(例如)用于分配的可用内存。 的malloc 是一个(标准)的库函数借此1MB,看着你想分配内存,切1MB的一部分,并把它送给你。对于簿记,它保持的未分配存储器的链表。在免费函数然后将此块被释放回的免费列表的,有效地释放内存(即使操作系统仍然没有得到任何它的背部,除非免费决定你有太多的记忆,实际上给它回OS)。

Let's say initially your program has 1MB (for example) available memory for allocation. malloc is a (standard) library function that takes this 1MB, looks at the memory you want to allocate, cut a part of the 1MB out and give it to you. For book-keeping, it keeps a linked-list of unallocated memories. The free function then adds the block being freed back to the free list, effectively freeing the memory (even though the OS still doesn't get any of it back, unless free decides that you have way too much memory and actually give it back to the OS).

只有当你运行你的1MB确实的malloc 竟向操作系统的更多内存。系统调用本身是依赖于平台。你可以看看这个答案例如

Only when you run out of your 1MB does malloc actually ask the operating system for more memory. The system call itself is platform dependent. You can take a look at this answer for example.

这是不是一个标准的功能,它可以通过各种方式,其中没有可能是有史以来调用任何系统功能来实现的(除非他们是不够好,增加你的堆栈大小,但你永远不知道)。

This is not a standard function, and it could be implemented in various ways, none of which probably ever call any system functions (unless they are nice enough to increase your stack size, but you never know).

什么的alloca 并(或等价的(C99)的标准可变长度阵列(VLA)这样做)是通过调整适当的寄存器来提高当前函数的栈帧(例如尤其 86中)。出现这种情况的任何变量是相同的堆栈帧,但位于可变长度数组后(或的alloca ED内存),那么将通过 EBP +解决size_of_vla +恒而不是历久弥新简单的 EBP +不变

What alloca does (or equivalently the (C99) standard variable length arrays (VLA) do) is to increase the stack frame of the current function by adjusting proper registers (for example esp in x86). Any variable that happens to be on the same stack frame but located after the variable length array (or allocaed memory) would then be addressed by ebp + size_of_vla + constant instead of the good old simple ebp + constant.

由于堆栈指针恢复到previous函数的函数返回时的帧(或一般在任何 {} 块的出口),任何堆栈存储器的alloca 特德会自动释放。

Since the stack pointer is recovered to the frame of the previous function upon function return (or generally on exit of any {} block), any stack memory allocated would be automatically released.

这篇关于堆栈上运行时内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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