stack和heap的区别
The difference between stack and heap memory allocation
Posted: 11th August 2010 by Tim in C, C++, Software Development
Tags: alloc, allocation, C, coding, free, heap, malloc, memory, stack, stack
overflow
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的区别的更多相关文章
- 面试题思考:Stack和Heap的区别
堆栈的概念: 堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈是个特殊的存储区,主要功能是暂时存放数据和地址,通常 ...
- stack,heap的区别
一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其 操作方式类似于数据结构中的栈. ...
- 面试题思考:Stack和Heap的区别 栈和堆的区别
堆栈的概念: 堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈是个特殊的存储区,主要功能是暂时存放数据和地址,通常 ...
- Stack与Heap的区别
申明:这里所说的栈和堆是程序内存管理中的栈和堆,而不是数据结构里的栈和堆. (1)保存的内容不同:栈里保存的是局部变量,而堆里保存的是动态申请的变量. (2)栈里的内存系统自动申请和释放,程序执行出申 ...
- JAVA中Stack和Heap的区别
http://m.blog.csdn.net/wl_ldy/article/details/5935528
- 图解.NET Stack和Heap的本质区别
现在越来越觉得对.NET基本概念的理解和掌握对于提升编程水平的重要性,先从.NET的 Stack(栈)和Heap(堆)说起,计算机的内存可以分为代码块内存,stack内存和heap内存.代码块内存是在 ...
- 【转】JVM运行原理及JVM中的Stack和Heap的实现过程
来自: http://blog.csdn.net//u011067360/article/details/46047521 Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’( ...
- JVM的stack和heap,JVM内存模型,垃圾回收策略,分代收集,增量收集
(转自:http://my.oschina.net/u/436879/blog/85478) 在JVM中,内存分为两个部分,Stack(栈)和Heap(堆),这里,我们从JVM的内存管理原理的角度来认 ...
- JVM运行原理及Stack和Heap的实现过程
Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’(.class文件,也就是0,1二进制程序),然后在OS之上的Java解释器中解释执行,而JVM是java的核心和基础,在ja ...
随机推荐
- (34)C#异常
一.异常的层次结构 二.异常格式 异常的一般格式 try { //可能会抛出异常的代码 } catch { //发现错误后会运行这里面的代码 } finally { //写不论是否出现异常都执行的代码 ...
- (转)小白科普, netty 有啥用?
随着移动互联网的爆发性增长,小明公司的电子商务系统访问量越来越大,由于现有系统是个单体的巨型应用,已经无法满足海量的并发请求,拆分势在必行. 在微服务的大潮之中, 架构师小明把系统拆分成了多个服务 ...
- mybatis-sql语句传参
MyBatis中的映射语句有一个parameterType属性来制定输入参数的类型.但是parameterType属性只可以写一个参数,所以如果我们想给映射语句传入多个参数的话,我们可以将所有的输入参 ...
- 7款外观迷人的HTML5/CSS3 3D特效按钮特效
下面我整理了7款外观都十分迷人的HTML5/CSS3 3D按钮特效,有几个还挺实用的,分享给大家. 1.CSS3超酷3D弹性按钮 按钮实现非常简单 之前我们分享过几款不错的CSS3 3D立体按钮,比如 ...
- JS数组 呼叫团里成员(使用数组元素) myarray[0]
呼叫团里成员(使用数组元素) 我们知道数组中的每个值有一个索引号,从0开始,如下图, myarray变量存储6个人的成绩: 要得到一个数组元素的值,只需引用数组变量并提供一个索引,如: 第一个人的 ...
- mongodb 3.2 yaml 配置详解及范例
mongodb3.x版本后就是要yaml语法格式的配置文件,下面是yaml配置文件格式如下:官方yaml配置文件选项参考:https://docs.mongodb.org/manual/ ... #c ...
- Lock方法是用于数据库的锁机制,
Lock方法是用于数据库的锁机制,如果在查询或者执行操作的时候使用: lock(true); 复制代码 就会自动在生成的SQL语句最后加上 FOR UPDATE或者FOR UPDATE NOWAI ...
- Python-数据类型内置方法(1)
目录 数字类型内置方法 整形(int) 浮点型(float) 字符串类型内置方法 优先掌握 需要掌握 了解 列表类型内置方法 优先掌握 需要掌握 数字类型内置方法 整形(int) 作用:年龄 定义:x ...
- Python-线程(2)
目录 GIL全局解释器锁 GIL 与 Lock 多进程 VS 多线程 死锁现象 递归锁 信号量 Semaphore 线程队列 GIL全局解释器锁 在Cpython解释器中,同一个进程下开启的多线程,同 ...
- input 的 placeholder 样式修改
input::-webkit-input-placeholder{ color:#999999; } input::-moz-placeholder{ /* Mozilla Firefox 19+ * ...