Objective C:
Memory, Variables, and
 Passing Data Around
     TN Valley Apple Developers
         Saturday CodeJam
       ...
Memory Layout of a Program’s
   Virtual Address Space
                                  Stack
                            ...
What’s the key difference?
  The size of the Text segment and Data segment are
  fixed; the size of these segments is dete...
Storing Data in Memory
Each memory segment where data can be
stored has a physical address, which is
represented with a he...
Checking the Address
Objective-C inherits a way from C to
find the address of a data variable – the
“&” (ampersand) symbol...
Printing out an Address
Memory addresses can be displayed by
formatted NSString objects using the
format specifier “%p”, w...
How can Objects and
Methods share information?
  By passing information to methods
  through message parameters, and
  ret...
Anatomy of a Method Signature
- (NSString*) movieName: (NSInteger*)movieIndex;

1        2           3           4        ...
Anatomy of a Method Signature
- (NSString*) movieName: (NSInteger*)movieIndex;

1        2           3           4        ...
Anatomy of a Message
[newMovie movieName:(NSInteger *)movieIndex]
       1            2               3                4
 ...
What are Class Methods?

 Class methods are methods that can be
 executed without instantiating an object
 of that class t...
Common Class Methods
All initialization methods for a class are “class
methods”
Class methods are also commonly utilized i...
Objective C:  Memory and Variables
Upcoming SlideShare
Loading in …5
×

Objective C: Memory and Variables

4,748 views

Published on

Objective-C: Memory and Variables

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,748
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
67
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

  • Text Segment - contains program’s executable code and read-only data (constants); allocated at compile time.

    Data Segment - contains read-write data (global variables) allocated at compile time. These exist for the life of the program.

    Heap - contains blocks of memory that can be dispensed dynamically on request. When more memory is required, the heap expands upward. In Objective-C, this is where objects and their variables reside. In C, this is allocated out with “malloc” calls and returned with “free”; in Objective-C, allocated out with “alloc/retain/copy”, and returned with “dealloc/release”.

    Stack - used for calling functions & subroutines; also stores local variables. Allocated out as needed for function calls; when a function or subroutine completes and the allocated memory is no longer needed, it is released back to the system and the size of the stack shrinks.





  • By looking at the method signatures in a header file, you can see the type of data that a method is expected to return when a message is sent to an object created from that class.

    If a method doesn’t return a value when one is expected, Xcode will issue a “warning” indicating that “control reaches end of non-void function” -- this means that the expected value was not returned.


  • The method signature will also contain information about the type of data that the method expects to receive from a message. These are called “parameters”

    The “label” assigned to each parameter can be used inside the method to access the data values passed by the message.
  • The parameters of a message are where data is handed off to a method when your code sends a message to a receiver (object or class).




  • Objective C: Memory and Variables

    1. 1. Objective C: Memory, Variables, and Passing Data Around TN Valley Apple Developers Saturday CodeJam September 11, 2010
    2. 2. Memory Layout of a Program’s Virtual Address Space Stack (grows downward) Stack Pointer Increasing Addresses Heap (grows upward) Data Segment Text Segment
    3. 3. What’s the key difference? The size of the Text segment and Data segment are fixed; the size of these segments is determined at compile time; space is allocated when the program begins executing, and persists until the program ends. The size of the Heap and Stack are determined at runtime, and memory needed for each is allocated out as needed and returned for reuse when program sections no longer need it – these grow/shrink as needed.
    4. 4. Storing Data in Memory Each memory segment where data can be stored has a physical address, which is represented with a hexadecimal number Data stored in a memory segment can be access two ways: Physical address (hexadecimal address) Pointer to the data variable
    5. 5. Checking the Address Objective-C inherits a way from C to find the address of a data variable – the “&” (ampersand) symbol, which creates a variable reference To find the memory address of a variable, precede the name with “&”: &variableName
    6. 6. Printing out an Address Memory addresses can be displayed by formatted NSString objects using the format specifier “%p”, which is a placeholder for a “pointer” value Remember, just like variable references, pointer variables contain the memory address of a variable as its value
    7. 7. How can Objects and Methods share information? By passing information to methods through message parameters, and returning information from methods after they have run
    8. 8. Anatomy of a Method Signature - (NSString*) movieName: (NSInteger*)movieIndex; 1 2 3 4 5 1 Indicates the type of method (class or instance) 2 Type of data returned from method 3 Method name (includes colon) 4 Type of data passed to method 5 Variable name (label) of data passed to method
    9. 9. Anatomy of a Method Signature - (NSString*) movieName: (NSInteger*)movieIndex; 1 2 3 4 5 1 Indicates the type of method (class or instance) 2 Type of data returned from method 3 Method name (includes colon) 4 Type of data passed to method 5 Variable name (label) of data passed to method
    10. 10. Anatomy of a Message [newMovie movieName:(NSInteger *)movieIndex] 1 2 3 4 1 Object name (the receiver) 2 method name(includes colon) 3 Parameter data type (may be optional) 4 Parameter variable name (label) Note: in this example, the method executed in response to the message returns a value (previous slide), so you would typically assign the returned value to a variable of some sort
    11. 11. What are Class Methods? Class methods are methods that can be executed without instantiating an object of that class type Class methods are denoted with a “+” preceding the method name in the method’s signature in the header file
    12. 12. Common Class Methods All initialization methods for a class are “class methods” Class methods are also commonly utilized in utility classes – for example, a method that contains the code for complex calculations that do not depend on the object type of the message sender to execute properly. A limitation of class methods is that they have no access to any of the instance variables of a class

    ×