发现做了这么多年的嵌入式开发,还有朋友搞不清楚stack & heap的区别,在阅读《Operating Systems_Three Easy Piece》时,里面的一段解释觉得非常好!
C programs use the stack for local variables, function parameters, and return addresses; the OS allocates this memory and gives it to the process. The OS will also likely initialize the stack with arguments; specifically, it will fill in the parameters to the main() function, i.e., argc and the argv array .
stack用于变量、函数参数、函数返回地址;
In C programs, the heap is used for explicitly requested dynamically-allocated data; programs request such space by calling malloc() and free it explicitly by calling free(). The heap is needed for data structures such as linked lists, hash tables, trees, and other interesting data structures. The heap will be small at first; as the program runs, and requests more memory via the malloc() library API, the OS may get involved and allocate more memory to the process to help satisfy such calls.
heap用于程序动态分配数据,比如malloc(),分配数据类型等,heap随着程序运行会分配越来越多的存储空间。
阅读(204585) | 评论(0) | 转发(0) |