Using Static in Objective-c

Note. My question is not related to this .

Also, that a variable that is not visible outside the file is declared in terms of memory allocation , is there any difference between declaring this (outside the method):

NSString *const kMyConstant = @"Hello World"; 

Or that:

 static NSString *const kMyConstant = @"Hello World"; 
+7
c static objective-c static-variables
source share
4 answers

In terms of memory allocation, there is no difference. Both variables are pointers to the same string constant, both are selected once, and in both cases their lifetime is the lifetime of the program.

+2
source share

No, no, it just affects the visibility of the object outside the compilation unit.

This can also be achieved with:

 __attribute__ ((visibility ("hidden")) NSString *const kMyConstant = @"Hello World"; 

Strike>

EDIT These are shoemakers; the visibility hidden attribute affects visibility outside the shared object, not the compilation unit.

+1
source share

I think this is a very interesting question, since it shows clear and understandable control of constants, on the one hand, and some obvious exceptions that Objective-C and clang include when processing constants of the NSString class.

I believe the following applies:

Above declarations and initializations from a question does not make any difference to memory management. It just doesn't exist. Constants are simply included in the package and are not distinguished in classical terms. The value of this value from the object class indicates the place of memory of the package, where the string constant. You can easily find this by comparing the address of such an NSString and its object class. The string address is very low, indicating a range of address addresses of the bundle. You can see that the addresses of the other lines that were initialized in the code indicate very different locations. Objective-C runtime does not perform any memory management on string constants, as it would be rather inconvenient to "release" or remove something from the package. So, if you play with this in a non-ARC environment, you will see that save and release are simply ignored.

To end this by answering the question: no, there is no difference in memory management in both cases. It is simply not done. Memory is allocated by the package and the OS frees up when the application terminates. It does not apply only to declarations and assignment of an explicit constant outside the implementation, but also inside any method.

+1
source share

It matters with the two above expressions, when you declare a variable with a static scope, it saves its assigned values ​​for multiple calls that are good for performance, in some cases, however, there is a lack of it as memory for the static variable is allocated once and stays there through the program until the application terminates. In addition, it is rather difficult to isolate statically defined variables. It will never free memory until the application terminates and you run the risk of using a whole bunch of memory and ARC will not help you here.

0
source share

All Articles