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)
…etc

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
i.

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:

objc_ref_1

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

objc_ref_2

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.

passbyvalue

Leave a Reply