About the Storage allocation

     It doesn't matter what programming language u use,it's all about the usage of variable---storage management.

1.Static & Dynamic

static: Global constants and the other information/data generated by compiler are allocated static storage. Static storage is built before any execution,and they would stay in memory throughout execution.

Advantage: use for public /global generally; instance object with the same class share static data/function

Disadvantage:can't be changed(lack of flexibility);static function can't manipulate non-static variables

dynamic: storage can be allocated when program is ruuning.

2.Heap

Within programming, if u need to get any storage,u can "new"/"malloc",but at the end of program,u have better "delete"/"free" these storage,only in this way could the program be secure(e.g. no memory leak). From above,we would know that the size of heap can't be detemined by one time generally.

advantage:dynamic storage management

disvadvantage:unsecure(forget to "delete /free"),ineffiency

3.Stack

Any information about local variables,parameters,state,etc. are allocated space on a stack,the same to heap ,its size can't be determined by compile time

advantage: support the recursion

disadvantage: inffiency(indirect addressing)

PS:文中若出现语法错误,请见谅并指出,大家可互相学习

原文地址:https://www.cnblogs.com/allenpengyu/p/3422851.html