iOS Development: Memory analysis for OC objects

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.