Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory address of array

Please help me out. I'm totally confused about in this memory address.

    NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3", nil];

    NSLog(@"Address of array =  %p",&arr);
    NSLog(@"Array's element Address =  %p",arr);
    NSLog(@"Address 0th element %p",[arr objectAtIndex:0]);

    NSLog(@"Address of array =%lx",(long)&arr);
    NSLog(@"Array's element Address =  %lx",(long)arr);
    NSLog(@"Address 0th element %lx",(long)[arr objectAtIndex:0]);

    **Output**

    Address of array =  0xbfffd804
    Array's element Address =  0x866f340
    Address 0th element 0x4748
    Address of array =bfffd804
    Array's element Address =  866f340
    Address 0th element 4748

I'm getting this output but i'm confused with second and third output because according to my knowldge second and third output should be same. So please any one can explain me. Thanks in advance.

like image 847
Dharmbir Singh Avatar asked Dec 13 '13 07:12

Dharmbir Singh


People also ask

What is the memory address of first array called?

The memory address of the first element of an array is called first address, foundation address, or base address. Because the mathematical concept of a matrix can be represented as a two-dimensional grid, two-dimensional arrays are also sometimes called "matrices".

What is memory in array?

A memory array is a linear data structure that stores a collection of similar data types at contiguous locations in a computer's memory. Memory arrays are categorized as one-dimensional arrays and multiple-dimensional arrays. Arrays are among the oldest data structures and are used in almost every computer program.

How is an array stored in memory?

An array stores its elements in contiguous memory locations. If You created the array locally it will be on stack. Where the elements are stored depends on the storage specification.


1 Answers

If this was a plain C array you might be right, but your arr is an instance of a class, not the base address of whatever data structure NSMutableArray uses to store its objects (which is probably not a plain C array, either).

like image 117
NSAdam Avatar answered Oct 12 '22 07:10

NSAdam