Const vs static NSStrings in Objective-C

These lines are in the implementation file above the @implementation .

 NSString * const aVar = @"aVarStringValue"; static NSString *aVar = @"aVarStringValue"; 

As far as I understand, the second static is allocated only once during the lifetime of the application, and this fact improves productivity.

But does this mean that it is essentially a memory leak since this memory block will never be released?

And does the first declaration of const distributed every time it is accessed?

+42
objective-c cocoa-touch cocoa
Nov 27 '09 at 23:51
source share
3 answers

static keyword in Objective-C (and C / C ++) indicates the visibility of a variable. A static variable (not in a method) can only be accessed in this particular .m file. On the other hand, a static local variable is allocated only once.

const , on the other hand, indicates that the link cannot be changed and / or reassigned; and is orthogonal in how it can be created (compilers can optimize consts, though).

It is worth noting that NSString literals are initialized and never destroyed in the life of the application. They are allocated in read-only memory.

+94
Nov 27 '09 at 23:59
source share

Staticity only changes the scope of the variable, not how it is declared or stored.

In both cases, the compiler will create a permanent version of the NSString instance, which is stored in the mach-o file. Thus, there is only one instance (note that you can change the behavior to force the dynamics to be dynamically created when mach-o loads, but there is one more instance).

static simply marks the aVar variable as visible within the compilation area - only the file. Without static you can update the string as extern NSString *aVar; in the header somewhere and access it from anywhere.

const is orthogonal and, in the case of an NSString reference, is almost completely irrelevant.

+49
Nov 28 '09 at 0:00
source share

To allow all possible discussions about the need for static and the position of const :

According to the C99 / GNU99 specification (which is commonly used for Objective-C code):

  • static

    • - storage class specifier

    • File-level scope objects have an external link by default

    • objects of a file-level area with a static qualifier have an internal relationship
  • const

    • is a type classifier (is part of a type)

    • the keyword applied to the immediate left instance - i.e.

      • MyObj const * myVar; - unqualified pointer to the type of object with established criteria

      • MyObj * const myVar; - constant pointer to unqualified type of object

    • Left use - applies to the type of the object, not the variable

      • const MyObj * myVar; - unqualified pointer to the type of object with specified content

SO:

static NSString * const myVar; - a constant pointer to an immutable string with internal communication.

The absence of the static will make the global name of the variable and can lead to name conflicts in the application.

+3
May 11 '16 at PM
source share



All Articles