栈和堆内存大小 [英] Size of stack and heap memory

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

问题描述


  

可能重复:结果
  什么,在哪里栈和堆?


对于在C程序内存布局的基本概念,我的理解是:


  • 语言使用两个主要数据结构

  • 堆栈中创建并存储在本地变量和子程序的记账数据

  • 堆是创建的用于存储程序的动态分配变量

  • 堆在本质上是变长的。(而不是在栈非常肯定)

  • 一般而言,这是编译器/语言的要求操作系统执行之前创建这些数据结构的责任。

问题


  • 什么是与创建堆栈/堆的初始大小?谁决定的?

  • 其中物理内存中创建它们?我看到的一般描述为 中的顶级地址创建栈和堆在低级别地址请elobarate这个


解决方案

  

堆栈在顶层地址创建和堆在
  低级别地址请elobarate此


这是一个神话。它可能在历史真相的基础。它有时可能与你的东西在现实生活中看到的共鸣。但它不是完全正确的。

这是很容易的探索,虽然:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;无效检查(INT深度){
    焦炭℃;
    字符* PTR =的malloc(1);
    的printf(堆栈在%P,堆在%P \\ N,和C,PTR);
    如果(深度LT; = 0)回报;
    检查(深度-1);
}诠释主(){
    检查(10);
    返回0;
}

在我的机器上我看到:

 在0x20010240在0x22ac3b栈,堆
在堆叠0x22ac0b,堆在0x200485b0
在堆叠0x22abdb,堆在0x200485c0
在堆叠0x22abab,堆在0x200485d0
在堆叠0x22ab7b,堆在0x200485e0
在堆叠0x22ab4b,堆在0x200485f0
在堆叠0x22ab1b,堆在0x20048600
在堆叠0x22aaeb,堆在0x20048610
在堆叠0x22aabb,堆在0x20048620
在堆叠0x22aa8b,堆在0x20048630
在堆叠0x22aa5b,堆在0x20048640

因此​​,堆栈下行的和堆是走上坡路(正如你所期望的基础上的神话),但堆栈具有较小的地址,他们不向对方(神话打掉)增长。

顺便说一句,我的检查函数是尾递归,并在一些实现一些编译器选项,您可能会看到堆栈不动的。它告诉你一些关于为什么标准并不强制这一切是如何工作的 - 如果它这样做是可能会无意中禁止有用的优化

Possible Duplicate:
What and where are the stack and heap?

With regard to the basic concepts of memory layout in a c program, I understand the that:

  • The language uses two primary data structures stack and heap.
  • Stack is created to store the local variables and book keeping data of subroutines
  • Heap is created to store the dynamically allocated variables of the program
  • Heap is of variable length in nature.(Not very sure on stack)
  • Normally it is the responsibility of the compiler/Language to request the OS to create these data structures before the execution.

Questions

  • What is the initial size with which a stack/heap is created? and who decides it?
  • Wherein physical memory are they are created? I see a general description as "Stack is created in the top-level-address and the heap at the low-level-address" Please elobarate this

解决方案

"Stack is created in the top-level-address and the heap at the low-level-address" Please elobarate this

This is a myth. It may have a basis in historical truth. It might sometimes resonate with things you see in real life. But it is not literally true.

It's easy enough to explore, though:

#include <stdlib.h>
#include <stdio.h>

void check(int depth) {
    char c;
    char *ptr = malloc(1);
    printf("stack at %p, heap at %p\n", &c, ptr);
    if (depth <= 0) return;
    check(depth-1);
}

int main() {
    check(10);
    return 0;
}

On my machine I see:

stack at 0x22ac3b, heap at 0x20010240
stack at 0x22ac0b, heap at 0x200485b0
stack at 0x22abdb, heap at 0x200485c0
stack at 0x22abab, heap at 0x200485d0
stack at 0x22ab7b, heap at 0x200485e0
stack at 0x22ab4b, heap at 0x200485f0
stack at 0x22ab1b, heap at 0x20048600
stack at 0x22aaeb, heap at 0x20048610
stack at 0x22aabb, heap at 0x20048620
stack at 0x22aa8b, heap at 0x20048630
stack at 0x22aa5b, heap at 0x20048640

So, the stack is going downwards and the heap is going upwards (as you might expect based on the myth), but the stack has the smaller address, and they are not growing toward each other (myth busted).

Btw, my check function is tail-recursive, and on some implementations with some compiler options you might see the stack not moving at all. Which tells you something about why the standard doesn't mandate how all this works -- if it did it might inadvertently forbid useful optimizations.

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

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