iOS Hardening Knowledge (1) Understanding Objective-c

Source: Internet
Author: User

always want to write a whole objective-c of the post, always have no time, so I want to make big knowledge small, small knowledge change small, write a little bit every day, to learn something to share with you, good nonsense not much to say .


1. A Dynamic language OC


Object-c (hereinafter referred to as OC) is an object-oriented language, which I'm afraid people who have studied programming know. But OC seems to be out of tune with other languages. Because it is a message structure language, not a function call. So it's a weird syntax, usually:

[ Object message ];

But the message-type language has one advantage, namely its dynamic nature. The code that executes at its runtime is dynamic, depending on the operating environment at the time. In a functional language, the code-compiling phase of the execution is determined. Functional language compilation requires a query function table to know exactly which code to run. In the message language, the compile time does not need to know which code to run, because it is always run until the time to find. It doesn't even need to know the type of the Message object, and this process is called "dynamic binding."


So OC runtime differs from other languages, OC important work is done in "runtime component", all the features of OC and memory management are completed in "Runtime Component", "Runtime component" is the dynamic library. And the code can combine all the code written by the developer. In this case, only the "run-time component" needs to be updated to improve program performance, and those functional languages need to be recompiled.




2, "Heap Memory" and "Stack Memory"

Unlike C + +, OC does not allow memory on the OC object to be allocated on the stack (stack) and can only be allocated to the heap.

C + + string str = "123"; Legal

OC nsstring str = @ "123"; Illegal


OC must point to an OC object with a pointer, such as:

nsstring* str = @ "123"; Legal

and everyone knows that the pointer's memory is allocated to the stack .


So for example:

nsstring* str2 = str;

This is just a copy of the pointer, and no new space is allocated.


Describe it in a picture:


Allocate the memory on the stack and the system cleans up automatically. The memory allocated to the heap is cleaned by the programmer. The OC itself implements the reference counting mode to manage memory.


Sometimes, however, you will find that some variable definitions do not contain "*". Then they may be allocated on the stack. (except for ID, because it is already a pointer)

OC is a superset of C, fully compatible with C so the basic types and structs are allowed to be allocated on the stack, for example:

Nsinteger cgfloat cgrect cgpoint int double BOOL and so on.


The cost of creating a struct is much smaller than the object. If you are not familiar with C language, then you have to work hard.



3. Reference count and Automatic reference count (ARC)

reference counting is a strategy for memory management, in short, each object needs to maintain an integer that records the number of times that the object is referenced, and if a new reference (a copy of the pointer) points to the object, the reference count +1, when a reference is lost to the point, the reference-1, When no reference to the object is pointed to, the object is destroyed.

Because OC only allows objects to be allocated on the heap, the reference count is appropriate for OC. But the reference count maintenance cost is slightly higher, and error-prone, too many reference counts, resulting in memory leaks, too little, there are wild pointers, for inexperienced programmers, it is difficult to balance, need to be familiar with the mechanism, and familiar with the corresponding API. So the advanced compiler supports automatic reference counting, the work of reference counting is given to the compiler, the compiler controls the reference count by detection, but it also has its own rules, which will be addressed later in the post.


I'll talk about it today, thanks for watching.


Today's Highlights:

OC is C super, increased object-oriented, OC uses dynamic binding message structure, only the runtime to know the type of object, and the code that needs to be executed, is not determined by the compiler.

Understand what the heap memory holds and what the stack memory holds.


iOS Hardening Knowledge (1) Understanding Objective-c

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.