Objective-c’s Addresses and Pass by References

In CPU, a switch is to describe a bit. A bit represent either 0 or 1. You switch on or off. 1 or 0. Thus, its called a switch.

Eight of these bits make up a byte. Each representation of these bits make up a representation. For example, in a byte, we can have:

00000000 (0)
00000001 (1)
00000010 (2)
00000011 (3)

So as you can see, if we were to make each representation a number, we can have up to
2^8-1 representations. where 8 is the number of bits.
Currently, the example is binary.

Hexadecimal Numbers is a more complex system than using just binary or decimal and is mainly used when dealing with computers and memory address locations.

By dividing a binary number up into groups of 4 bits, each group or set of 4 digits can now have a possible value of between “0000” (0) and “1111” ( 8+4+2+1 = 15 ) giving a total of 16 different number combinations from 0 to 15. Don’t forget that “0” is also a valid digit.

Thus, a 4 bit binary number is a hex digit. If a system has 32 bits, we can say it has 8 hex digits.

If we were to work with hex, each hex digit would have 16 representations: 0123456789abcdef

In computers, data is represented by an address. An address is a hex representation.

The address of a variable is the location in memory where the value for that variable is stored.
To get the variable’s address, you use the & operator:

Notice the %p token. That’s the token you can replace with a memory address. Build and run
the program. You’ll see something like:

i stores its value at 0xbffff738

its in hex which is calculated like this

Storing Addresses in pointers

Declare a new variable named addressOfI that is a pointer to an int. Assign it the address of

Getting the data at the address

Notice that the asterisk is used two different ways The first is in the declaration where you declare the variable addressOfI to be an int *. That is, it is a pointer to a place where an int can be stored.
The second is where you read the int value that is stored at the address stored in addressOfI. (Pointers are also called references. Thus, using the pointer to read data at the address is sometimes called dereferencing the pointer.)
You can also use the * operator on the left-hand side of an assignment to store data at a particular address:

Passing the Reference…by value

This is known as pass-by-reference. That is, you supply an address (also known as “a reference”), and the function puts the data there.

Changes would take place inside method ONLY if you do not move pointer

If you don’t move the parameter pointer, it would work.

For example:


In detail, here’s why its pass by value of the reference:


If you run it, you’ll see that the ptr inside the function points away to another fresh allocated integer. The outside did not move.

In order to really use a reference (similar to that of C++) to modify the data passed into a method, you need to use a double ptr:

Using pointer of pointer to make changes permanent from inside the method

Pass by Copy

As you can see, the address of our variable is different outside, than the address of the parameter variable inside test2.

Basically you just copy the value in. The parameter is basically a newly created variable on the stack with a different address, but has the same value as what you pass in.



Every function can have local variables. Local variables are variables declared inside a function. They exist only during the execution of that function and can only be accessed from within that function. For example, imagine that you were writing a function that computed how long to cook a turkey. It might look like this:

necessaryMinutes is a local variable. It came into existence when showCookTimeForTurkey() started to execute and will cease to exist once that function completes execution.


The parameter of the function, pounds, is also a local variable. A parameter is a local variable that has been initialized to the value of the corresponding argument. A function can have many local variables, and all of them are stored in the frame for that function. Think of the frame as a blackboard that you can scribble on while the function is running. When the function is done executing, the blackboard is discarded.

Programmers say, “When a function is called, its frame is created on top of the stack. When the function finishes executing, its frame is popped off the stack and destroyed.”


Stack Example

  • necessaryMinutes = 135
  • pounds = 9
  • showTurkeyCookTime()

  • turkeyWeight = 9
  • gibletsWeight = 1
  • totalWeight = 10
  • main()

Notice that pounds, the single parameter of showCookTimeForTurkey(), is part of the frame. Recall that a parameter is a local variable that has been assigned the value of the corresponding argument. For this example, the variable turkeyWeight with a value of 9 is passed as an argument to showCookTimeForTurkey(). Then that value is assigned to the parameter pounds and copied to the function’s frame.

reference: http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/stack.html


When a program starts executing, a certain contiguous section of memory is set aside for the program called the stack.

Let’s look at a stack.


The stack pointer is usually a register that contains the top of the stack. The stack pointer contains the smallest address x such that any address smaller than x is considered garbage, and any address greater than or equal to x is considered valid.

In the example above, the stack pointer contains the value 0x0000 1000, which was somewhat arbitrarily chosen.

The shaded region of the diagram represents valid parts of the stack.

It’s useful to think of the following aspects of a stack.

stack bottom The largest valid address of a stack. When a stack is initialized, the stack pointer points to the stack bottom.

stack limit The smallest valid address of a stack. If the stack pointer gets smaller than this, then there’s a stack overflow (this should not be confused with overflow from math operations).

Other sections of memory are used for the program and for the heap (the section of memory used for dynamic memory allocation).

no stack languages

Stackless Languages?
Is the stack absolutely necessary? FORTRAN 77 (77 refers to the year 1977) did not use a stack. Basically, each function had a section of memory for its own arguments and data. Because there were no stacks, you couldn’t write recursive functions in FORTRAN 77. However, it goes to show you that a stack isn’t always needed.

There’s also a complicated language feature called continuations (which appear in languages like Scheme). Continuations can be coded in a way that doesn’t require a stack, and in effect, acts like a queue.

Nevertheless, the vast majority of languages that implement functions use the stack.

threads and stacks

To what extent are they controlled by the OS or language runtime?

The OS allocates the stack for each system-level thread when the thread is created. Typically the OS is called by the language runtime to allocate the heap for the application.

What is their scope?

The stack is attached to a thread, so when the thread exits the stack is reclaimed. The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits.

What determines the size of each of them?

The size of the stack is set when a thread is created. The size of the heap is set on application startup, but can grow as space is needed (the allocator requests more memory from the operating system).

What makes one faster?

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free. Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor’s cache, making it very fast. Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, i.e. each allocation and deallocation needs to be – typically – synchronized with “all” other heap accesses in the program.

iPad: add a popover view on Navigation Bar

In your app delegate, have property for the current ViewController class:

Then in our didFinishLaunchingWithOptions method, we allocate this main ViewController, which we will insert into a newly created UINavigationController variable. We create this new UINavigationController below:

This way, our root is this navigation controller. And the navigation controller’s first page is our view controller.

View Controller


Run the demo and you should have a yellow popover window appear.
Now let’s put a table that has 3 colors to choose from in there instead.

The key is that we create a viewcontroller that extends from UITableViewController. when the table view controller gets a click on a entry, it sends its corresponding UI uiviewcontroller messages via a delegate.

ColorPickerDelegate and ColorPickerTableViewController

Go Back to your ViewController and change/add the following methods:

Java is always pass by value

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references and those references are passed by value.

It goes like this:



The Java Spec says that everything in Java is pass-by-value. There is no such thing as “pass-by-reference” in Java.

The key to understanding this is that something like

is not a Dog; it’s actually a pointer to a Dog.

What that means, is when you have

you’re essentially passing the address of the created Dog object to the foo method.

(I say essentially because Java pointers aren’t direct addresses, but it’s easiest to think of them that way)

Suppose the Dog object resides at memory address 42. This means we pass 42 to the method.

if the Method were defined as

let’s look at what’s happening.

the parameter someDog is set to the value 42
at line “AAA”
someDog is followed to the Dog it points to (the Dog object at address 42)
that Dog (the one at address 42) is asked to change his name to Max
at line “BBB”
a new Dog is created. Let’s say he’s at address 74
we assign the parameter someDog to 74
at line “CCC”
someDog is followed to the Dog it points to (the Dog object at address 74)
that Dog (the one at address 74) is asked to change his name to Rowlf
then, we return
Now let’s think about what happens outside the method:

Did myDog’s ADDRESS change?

There’s the key.

Keeping in mind that myDog is a pointer, and not an actual Dog, the answer is NO. myDog still has the value 42; it’s still pointing to the original Dog. ..However, the data has changed to MAX.

Objective C parameter passing


Jared P is wrong, objective-C only support passing parameters by value. The problem here has probably been fixed already (Since this question is more than a year old) but I need to clarify some things regarding arguments and Objective-C.

Objective-C is a strict superset of C which means that everything C does, Obj-C does it too.

By having a quick look at Wikipedia, you can see that Function parameters are always passed by value

Objective-C is no different. What’s happening here is that whenever we are passing an object to a function (In this case a UILabel *), we pass the value contained at the pointer’s address.

Whatever you do, it will always be the value of what you are passing. If you want to pass the value of the reference you would have to pass it a **object (Like often seen when passing NSError).

This is the same thing with scalars, they are passed by value, hence you can modify the value of the variable you received in your method and that won’t change the value of the original variable that you passed to the function.

Here’s an example to ease the understanding:

If you wanted to be able to modify i, you would have to pass the value of the reference by doing the following:


C does not support pass-by-reference and Objective-C, being a strict superset of C doesn’t either.

In C (and Objective-C) you can simulate pass-by-reference by passing a pointer, but it’s important to remember that you’re still technically passing a value, which happens to be a the value of a pointer.

So, in Objective-C (and C, for the matter) there is no concept of reference as intended in other languages (such as C++ or Java).

This can be confusing, so let me try to be clearer (I’ll use plain C, but – again – it doesn’t change in Objective-C)

Objective properties


  1. http://stackoverflow.com/questions/8927727/objective-c-arc-strong-vs-retain-and-weak-vs-assign?rq=1
  2. http://stackoverflow.com/questions/11013587/differences-between-strong-and-weak-in-objective-c
  • atomic (default)
  • nonatomic
  • strong=retain (default)
  • weak
  • retain
  • assign (default)
  • unsafe_unretained
  • copy
  • readonly
  • readwrite (default)
  • strong (iOS4 = retain )

    it says “keep this in the heap until I don’t point to it anymore”
    in other words ” I’am the owner, you cannot dealloc this before aim fine with that same as retain”
    You use strong only if you need to retain the object.

    By default all instance variables and local variables are strong pointers.

    We generally use strong for UIViewControllers (UI item’s parents)
    strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object. ARC automatically releases it for you when you are done with it.Using the keyword strong means that you own the object.


    it says “keep this as long as someone else points to it strongly”
    the same thing as assign, no retain or release
    A “weak” reference is a reference that you do not retain.
    We generally use weak for IBOutlets (UIViewController’s Childs).This works because the child object only needs to exist as long as the parent object does.
    a weak reference is a reference that does not protect the referenced object from collection by a garbage collector.
    Weak is essentially assign, a unretained property. Except the when the object is deallocated the weak pointer is automatically set to nil
    Example :

    Strong & Weak Explanation

    by BJ Homer

    Imagine our object is a dog, and that the dog wants to run away (be deallocated).

    Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached.

    Weak pointers, on the other hand, are like little kids pointing at the dog and saying “Look! A dog!” As long as the dog is still on the leash, the little kids can still see the dog, and they’ll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it.

    As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out.

    When we use weak?

    The only time you would want to use weak, is if you wanted to avoid retain cycles (e.g. the parent retains the child and the child retains the parent so neither is ever released).

    retain = strong

    it is retained, old value is released and it is assigned retain specifies the new value should be sent retain on assignment and the old value sent -release
    retain is the same as strong.

    apple says if you write retain it will auto converted/work like strong only.


    assign is the default and simply performs a variable assignment
    assign is a property attribute that tells the compiler how to synthesize the property’s setter implementation
    I would use assign for C primitive properties and weak for weak references to Objective-C objects.

    Strong vs Weak reference

    A strong reference (which you will use in most cases) means that you want to “own” the object you are referencing with this property/variable. The compiler will take care that any object that you assign to this property will not be destroyed as long as you (or any other object) points to it with a strong reference. Only once you set the property to nil will the object get destroyed (unless one or more other objects also hold a strong reference to it).

    In contrast, with a weak reference you signify that you don’t want to have control over the object’s lifetime.

    The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to nil.

    The most frequent use cases of weak references in iOS are:

    • delegate properties, which are often referenced weakly to avoid retain cycles
    • subviews/controls of a view controller’s main view because those views are already strongly held by the main view

    Atomic vs Nonatomic

    atomic vs. nonatomic refers to the thread safety of the getter and setter methods that the compiler synthesizes for the property. atomic (the default) tells the compiler to make the accessor methods thread-safe (by adding a lock before an ivar is accessed):

    and nonatomic does the opposite. The advantage of nonatomic is slightly higher performance.
    On iOS, Apple uses nonatomic for almost all their properties so the general advice is for you to do the same.

Dismiss TextView or TextField

1) When clicking ‘Done’

Put protocol UITextViewDelegate and/or UITextFieldDelegate in there…

set up the variable….make sure you add target for the method UIControlEventEditingDidEndOnExit:

Implement the method:

2) To dismiss Keyboard on tap of the View:

view controller’s header:

view controller’s viewDidLoad:

view controller:

Finally, follow this link http://rickytsao.com/?p=1299
in order to cancel keyboard by tapping anywhere outside of it.