2014年4月27日 星期日

指標與記憶體(Pointers and Memory)

有四種指標變數,對應三種記憶體

一、Static/Global
  1. Static:作用範圍:被宣告的function中。生命週期:等同於這支應用程式。
  2. Global:作用範圍:所有的function都可存取。生命週期:等同於這支應用程式。
二、Automatic(Local)
作用範圍:被宣告的function中。生命週期:在這個function被呼叫的時候。
三、Dynamic
作用範圍:由指向這塊heap的指標所決定。生命週期:直到這塊memory被釋放。 

//關於Static的作用範圍,Objective-C跟C有些不同,請參考下列資料
Variable storage class specifiers are used when declaring a variable to give the compiler information about how a variable is likely to be used and accessed within the program being compiled. So far in this chapter we have actually already looked at two storage class specifiers in the form of extern and static. A full list of variable storage class specifiers supported by Objective-C is as follows:
  • extern - Specifies that the variable name is referencing a global variable specified in a different source file to the current file.
  • static - Specifies that the variable is to be accessible only within the scope of the current source file.
  • auto - The default value for variable declarations. Specifies the variable is to be local or global depending on where the declaration is made within the code. Since this is the default setting this specifier is rarely, if ever, used.
  • const - Declares a variable as being read-only. In other words, specifies that once the variable has been assigned a value, that value will not be subsequently changed.
  • volatile - Specifies that the value assigned to a variable will be changed in subsequent code. The default behavior for variable declarations.

沒有留言:

張貼留言