stack和heap的区别


       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、程序代码区—存放函数体的二进制代码。

 

转载于:https://www.cnblogs.com/eaglediao/p/7136550.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值