A common question amongst coders new to C or C++
relates to the difference between stack and heap memory allocation.
The answer lies in how the code is executed at the very lowest
level.

When a program is executed, each thread is
allocated a limited amount of stack space. The stack holds
information used by the program, including the raw byte code
executed on the processor.

 Variables
allocated on the stack, or automatic variables, are stored directly
to this memory.
Access to this memory is very fast, and
it’s allocation is dealt with when the program is compiled. Large
chunks of memory, such as very large arrays, should not be
allocated on the stack to avoid overfilling the stack memory (known
as stack overflow). Stack variables only exist in the block of
code in which they were declared
. For example:



void a()
{
    if(true)
{
        int
x = 0;
    }

x
= 1;
}      
     
     
     
     
     
     
     
     
     
     
     
     
     
   
 In
this code, x is
allocated on the stack. This value is not available outside of
the if() block,
so attempting to access the variable outside of the block, as
above, result in a compilation error.

Variables allocated on the heap, or dynamic
variables, have their memory allocated at run time
(ie: as the
program is executing). Accessing this memory is a bit
slower, but the heap size is only limited by the size of
virtual memory (ie: RAM and swap space). This memory remains
allocated until explicitly freed by the program
and, as a
result, may be accessed outside of the block in which it was
allocated. For example:


int *x;
void b()
{
    if(true)
{
        x
= malloc(sizeof(int));
    }

*x
= 1;
}      
     
     
     
     
     
     
     
     
     
     
     
     
     
     
   
void
c()      
     
     
     
     
     
     
     
     
     
     
     
     
     
   
 
{
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
   

free(x);      
     
     
     
     
     
     
     
     
     
     
     
     
     
 
}
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
 
  
In
this example, memory for the variable x is
allocated when b() is
called, and remains in memory until c() is
called. Notice how we can set the value
of x outside
of the if() block
in which it is allocated.

In summary, temporary variables should be
allocated on the stack.
It’s less mucking around with memory
allocation, the code is easier to read, the memory is accessed
faster and the program does not need to allocate the memory on the
fly. For large variables or arrays whose size may vary, heap
memory allocation is your friend.
Just remember to free all
of the memory allocated
or you’ll end up with memory
leaks.

一个由c/C++编译的程序占用的内存分为以下几个部分 1、栈区(stack)— 由编译器自动分配释放
,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。 2、堆区(heap) — 一般由程序员分配释放,
若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表。
3、全局区(静态区)(static)—,全局变量静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域,
未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 - 程序结束后由系统释放。4、文字常量区 —常量字符串就是放在这里的。
程序结束后由系统释放 。5、程序代码区—存放函数体的二进制代码。

stack和heap的区别的更多相关文章

  1. 面试题思考:Stack和Heap的区别

    堆栈的概念: 堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈是个特殊的存储区,主要功能是暂时存放数据和地址,通常 ...

  2. stack,heap的区别

    一个由C/C++编译的程序占用的内存分为以下几个部分    1.栈区(stack)—   由编译器自动分配释放   ,存放函数的参数值,局部变量的值等.其    操作方式类似于数据结构中的栈.    ...

  3. 面试题思考:Stack和Heap的区别 栈和堆的区别

    堆栈的概念: 堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈是个特殊的存储区,主要功能是暂时存放数据和地址,通常 ...

  4. Stack与Heap的区别

    申明:这里所说的栈和堆是程序内存管理中的栈和堆,而不是数据结构里的栈和堆. (1)保存的内容不同:栈里保存的是局部变量,而堆里保存的是动态申请的变量. (2)栈里的内存系统自动申请和释放,程序执行出申 ...

  5. JAVA中Stack和Heap的区别

    http://m.blog.csdn.net/wl_ldy/article/details/5935528

  6. 图解.NET Stack和Heap的本质区别

    现在越来越觉得对.NET基本概念的理解和掌握对于提升编程水平的重要性,先从.NET的 Stack(栈)和Heap(堆)说起,计算机的内存可以分为代码块内存,stack内存和heap内存.代码块内存是在 ...

  7. 【转】JVM运行原理及JVM中的Stack和Heap的实现过程

    来自: http://blog.csdn.net//u011067360/article/details/46047521 Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’( ...

  8. JVM的stack和heap,JVM内存模型,垃圾回收策略,分代收集,增量收集

    (转自:http://my.oschina.net/u/436879/blog/85478) 在JVM中,内存分为两个部分,Stack(栈)和Heap(堆),这里,我们从JVM的内存管理原理的角度来认 ...

  9. JVM运行原理及Stack和Heap的实现过程

    Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’(.class文件,也就是0,1二进制程序),然后在OS之上的Java解释器中解释执行,而JVM是java的核心和基础,在ja ...

随机推荐

  1. Andriod Fragment 的作用和基本用法

    1.什么是Fragment: Fragment (片段)在Google Android 开发指南中的解释是:片段是Activity中的一部分,一个Activity中可以有多个Fragment.一个Fr ...

  2. ASP.NET自定义Validform的datatype

    1.定义 <script type="text/javascript"> $(function () { $("#aa").Validform({ ...

  3. selenium基础(获取验证信息-断言)

    获取验证信息 实际结果与预期结果进行比较称之为断言 通过获取title.URL.text等信息进行断言 text方法用于获取标签对之间的文本信息 from selenium import webdri ...

  4. iOS开发系列-Block本质篇

    概述 在iOS开发中Block使用比较广泛,对于使用以及一些常规的技术点这里不再赘述,主要利用C++角度分析Block内部数据底层实现,解开开发中为什么这样编写代码解决问题. Block底层结构窥探 ...

  5. JS流程控制语句 反反复复(while循环) 和for循环有相同功能的还有while循环, while循环重复执行一段代码,直到某个条件不再满足。

    反反复复(while循环) 和for循环有相同功能的还有while循环, while循环重复执行一段代码,直到某个条件不再满足. while语句结构: while(判断条件) { 循环语句 } 使用w ...

  6. 记录:使用springboot的cors和vue的axios进行跨域

    一.编写一个配置类,并且注册CorsFilter: 注意允许跨域的域名不要写错 @Configuration public class ZysuyuanCorsConfiguration { @Bea ...

  7. 洛谷P3834【模板】可持久化线段树 1(主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

  8. react+redux+react-redux练习项目

    一,项目目录 二.1.新建pages包,在pages中新建TodoList.js: 2.新建store包,在store包中新建store.js,reducer.js,actionCreater.js, ...

  9. 2017.1.16【初中部 】普及组模拟赛C组总结

    2017.1.16[初中部 ]普及组模拟赛C组 这次总结我赶时间,不写这么详细了. 话说这次比赛,我虽然翻了个大车,但一天之内AK,我感到很高兴 比赛 0+15+0+100=115 改题 AK 一.c ...

  10. C++ 判断是否为邮箱格式

    总结了一下合法的email地址格式如下: 1. 首字符必须用字母,而且其它的字符只能用26个大小写字母.0~9及_-.@符号 2. 必须包含一个并且只有一个符号“@” 3. @后必须包含至少一个至多三 ...