FAQ (1)

Source: Internet
Author: User

1. Answer the retaincount value of person and explain why
Person * Per = [[person alloc] init]; The retaincount value of person is 1self. Person = per;
When self. Person is used, if it is assign, The retaincount value of person remains unchanged. If it is still 1, the value of retaincount of retain person is increased by 1 to 2.
If yes, the retaincount value of copy person remains unchanged and is still 1

2. Is there any problem with this code: @ implementation person
-(Void) setage :( INT) newage {

Self. Age = newage ;}

@ End

It will be an endless loop and will be called again! Change self. Age to _ age;

And the writing is not standard: The newage In the setter method should be age

3. What is the problem with this code and how to modify it?
For (INT I = 0; I <somelargenumber; I ++) {nsstring * string = @ "ABC ";
String = [String lowercasestring];
String = [String stringbyappendingstring: @ "XYZ"]; nslog (@ "% @", string );
}
Memory leakage may occur.
After modification:
For (INT I = 0; I <1000; I ++ ){

NSAID utoreleasepool * pool1 = [[NSAID utoreleasepool alloc] init]; nsstring * string = @ "ABC ";
String = [String lowercasestring];
String = [String stringbyappendingstring: @ "XYZ"]; nslog (@ "% @", string );

// Release the pool

[Pool1 drain];}

Extended: Stack differences:

(1) management method: For stacks, it is automatically managed by the compiler without manual control. For the heap, the release work is controlled by the programmer and memory leak is easily generated. (2) Application size: the space available from the stack is small. The heap is a data structure extended to the high address and is a non-sequential memory area. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large. (3) fragmentation problem: for the heap, frequent New/delete operations will inevitably lead to discontinuous memory space, resulting in a large number of fragments and reduced program efficiency. For the stack, this problem will not exist, because the stack is an advanced and outgoing queue, they are so one-to-one correspondence, it is impossible to have a memory block popped up from the middle of the stack (4) allocation Method: The heap is dynamically allocated without static allocation. There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is completed by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the stack dynamic allocation is different from the heap dynamic allocation. Its Dynamic Allocation is released by the compiler without manual implementation. (5) Allocation Efficiency: the stack is the data structure provided by the machine system, and the computer will provide support for the stack at the underlying layer: allocate a dedicated register to store the stack address, the output stack of the Pressure Stack has dedicated Command Execution, which determines the high efficiency of the stack. The heap is provided by the C/C ++ function library, and its mechanism is very complicated. 4. Write a constructor

+ (ID) person {

Person * person = [person alloc] init];
Return [person autorelease]; Note: autorelease is not required for arc.

}

5. Extract the string "20 | http://www.baidu.com", "|" character before and after the data, output them respectively.

Nsstring * STR = @ "20 | http://www.baidu.com ";
Nsarray * array = [STR componentsseparatedbystring: @ "|"]; // This is the truncated string output separately.

For (INT I = 0; I <[array count]; ++ I) {nslog (@ "% d = % @", I, [array objectatindex: I]);

}


FAQ (1)

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.