实例说明heap和stack在程序运行中的作用
2011-06-02 21:14
399 查看
http://www.cs.jcu.edu.au/Subjects/cp2003/1997/foils/heapAndStack/heapAndStack.htmlHeap vs. Stackvisual depiction of run-time storageheap freelist - list of free space on allocation - memory manager finds space and marks it as used changing freelist on deallocation - memory manager marks space as free changing freelist memory fragmentation - memory fragments into small blocks over lifetime of program garbage collection - coalesce fragments, possibly moving objects (must be careful of pointers when moving!) stack clean and efficient support for nested functions and recursion central concept is stack frame (also called activation record), includes visual depiction of frame parameters return address - where to begin execution when function exits dynamic link - pointer to caller's stack frame static link - pointer to lexical parent (for nested functions) return value - where to put the return value local variables local work space - for temporary storage of results function call - push stack frame function exit - pop stack frame visual depiction of stack calls example int x; /* static storage */ void main() { int y; /* dynamic stack storage */ char *str; /* dynamic stack storage */ str = malloc(100); /* allocates 100 bytes of dynamic heap storage */ y = foo(23); free(str); /* deallocates 100 bytes of dynamic heap storage */ } /* y and str deallocated as stack frame is popped */ int foo(int z) { /* z is dynamic stack storage */ char ch[100]; /* ch is dynamic stack storage */ if (z == 23) foo(7); return 3; /* z and ch are deallocated as stack frame is popped, 3 put on top of stack */ } at the start of the program after the first call to foo after the second call to foo Deadstorage objects in stack storage die (are |
相关文章推荐
- Java中内存中的Heap、Stack与程序运行的关系
- Java中内存中的Heap(堆)、Stack(栈)与程序运行的关系
- Java中内存中的Heap(堆)、Stack(栈)与程序运行的关系
- Java中内存中的Heap、Stack与程序运行的关系
- Java中内存中的Heap(堆)、Stack(栈)与程序运行的关系
- [Android实例] Android实现开机自动运行程序
- ★实验10-2 1. 编写程序测试sigaction()函数。 2. sa_flags 设置为以下标志时,程序的行为有何不同。 对比程序运行情况予以说明。 SA_SIGINFO SA_NOCLDST
- 各种语言Mutex(互斥)实现程序单实例运行
- C# Winform程序只允许运行一个实例
- C++程序运行时内存布局之----------简单类实例,成员变量,成员函数,静态成员变量,静态成员函数
- 让Java程序只运行一个实例
- 转:linux设备驱动程序设计中的程序样例运行的一点说明(编译Linux内核)
- 让 .Net 程序 脱离 .net framework框架 运行(源代码实现说明)
- 如何让程序只运行一次实例
- 仅运行一个程序的单个实例
- 让程序只能被运行一个实例
- C#只能运行一个实例程序的方法
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C++程序运行一个实例(互斥量法)
- C++Builder 程序只运行一个实例