您的位置:首页 > 编程语言 > C语言/C++

C++中关于variable 内存分配的问题.

2015-02-20 06:47 381 查看
https://github.com/jfl913/Notes/commit/02c7410b9bfca50f33b1999668f2dffeade6203f
  +The Stack
  +> When a method (or function) is executed, a chunk of memory is allocated from a part of memory called the stack. This chunk of memory is called a frame,
**and the frame stores the values for variables declared inside the method**. A variable declared inside a method is called a local variable.
  +
  +The Heap
  +
  +> The heap, on the other hand, is where all Objective-C
**objects** live. It is a giant heaping mess of
**objects**. You use pointers to keep track of where those
**objects** are stored in the heap.
  +>
  +> When you send the alloc message to a class, a chunk of memory is allocated from the heap.
  +
  +总结:**Stack里面存放的是`local variable`(局部变量),Heap里面存放的是`objects`(对象)**。
  +
  +我也从网上查到了由C/C++编译的程序占用的内存分为的以下几个部分:
  +
  +> 1. 栈区(stack)— 由**编译器**自动分配释放,存放函数的**参数值**,**局部变量的值**等。其
  + 操作方式类似于数据结构中的栈。
  +
2. 堆区(heap) — 一般由**程序员**分配释放,若程序员不释放,程序结束时可能由OS回
  + 收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表,呵呵。
  +
3. 全局区(静态区)(static)—,全局变量和静态变量的存储是放在一块的,初始化的
  + 全局变量和静态变量在一块区域,未初始化的全局变量和未初始化的静态变量在相邻的另
  + 一块区域。 - 程序结束后由系统释放。
  +
4. 文字常量区—常量字符串就是放在这里的。程序结束后由系统释放
  +
5. 程序代码区—存放函数体的二进制代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: