Recently to start looking for internship units, do a pen test, see the Great God interview experience, found that they have to learn something really there are many, although also done a few projects, but really took a look at the pen, only to find themselves on the basis of this aspect of things, indeed a little neglect, so recently opened the mode of evil.
These days in the study of iOS memory analysis this piece, only to find that they have neglected so many important things, here to share with you.
Take a look at the code first:
nsstring* s;
s = [[NSString alloc] initwithstring:@ "Hello iOS"];
This is the simplest string definition, but seemingly simple, there are many things that may be overlooked
1. Here s is actually a pointer, in OC except the basic data type of variable type is a pointer type, OC objects are also manipulated by pointers.
2. In the first paragraph the code defines a pointer variable of type nsstring, but does not point to any object, the second code alloc a NSString object and then makes S point to it and begins its subsequent operation through S.
3. Here s the memory space in the stack memory, "Hello iOS" in heap memory.
The following illustration illustrates
Let's look at one more example
@interface c:nsobject{ int A; int b; } @end int Main (intconstChar *argv[]) { *c1 = [[C alloc] INIT]; *C2 = [[C alloc]init]; }
Distribution of memory
As you can see here, the object pointer created in OC is stored in the stack memory, while the memory of the Alloc object is in heap memory, and the pointer in the stack memory points to the object in the heap memory, and then accesses the object and its variables through pointers in the stack memory, which means In fact, when we created an object, we actually took advantage of the two-part memory space.
iOS Development: Memory analysis for OC objects