alloc

Why are alloc and init called separately in Objective-C?

烈酒焚心 提交于 2019-11-27 21:21:13
Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could someone explain to me why I always have to first allocate and then initialize an instance? Couldn't this be done in the init methods like this: + (MyClass*)init { MyClass *instance = [MyClass alloc]; [instance setFoo:@"bla"]; return instance; } + (MyClass*)initWithString:(NSString*)text { MyClass *instance = [MyClass init]; [instance setFoo:text]; return instance; } ... Is this just a relict from the old C days or is there something that I'm not seeing? I know this isn't a problem as I could as well always call

What's the advantage of using std::allocator instead of new in C++?

醉酒当歌 提交于 2019-11-27 18:40:22
I've just read about std::allocator . In my opinion, it is more complicated to use it instead of using new and delete . With allocator we must explicitly allocate heap memory, construct it, destroy it, and then finally deallocate the memory. So why was it created? In which cases can it be used and when should it be used instead of new and delete? Benjamin Lindley std::allocator is the default memory allocator for the standard library containers, and you can substitute your own allocators. This allows you to control how the standard containers allocate memory. But I don't think that your

is it necessary to call pointer = NULL when initializing?

妖精的绣舞 提交于 2019-11-27 15:06:04
问题 when I create a pointer to certain struct, do I have to set it to NULL, then alloc it then use it? and why? 回答1: No, there is no requirement (as far as the language is concerned) to initialize a pointer variable to anything when declaring it. Thus T* ptr; is a valid declaration that introduces a variable named ptr with an indeterminate value. You can even use the variable in certain ways without first allocating anything or setting it to any specific value: func(&ptr); 回答2: No, you don't have

Lazy instantiation in Objective-C/ iPhone development

时光毁灭记忆、已成空白 提交于 2019-11-27 11:41:50
Quick question... Well I understand that all properties start out as nil in Objective-C and that sending a message to nil does nothing, therefore you must initialize using [[Class alloc] init]; before sending a message to a newly created property. However, what about if I'm not sending messages to this property or if I set the property using self.property = something? Do I need to alloc init in these cases as well? Also, do UI properties start out as nil as well, such as a UILabel property that you drag out from your storyboard? Do these need alloc init? Thanks to all who answer Jason C.

what is difference between alloc and allocWithZone:?

﹥>﹥吖頭↗ 提交于 2019-11-27 09:24:58
问题 From forum discussion , seem like that the big difference is performance factor, allocWithZone: will alloc memory from particular memory area, which reduce cost of swapping. In practice, almost get no chance to use allocWithZone: , anyone can give simple example to illustrate which case to use allocWithZone: ? Thanks, 回答1: When one object creates another, it’s sometimes a good idea to make sure they’re both allocated from the same region of memory. The zone method (declared in the NSObject

Lazy instantiation in Objective-C/ iPhone development

不打扰是莪最后的温柔 提交于 2019-11-26 15:41:16
问题 Quick question... Well I understand that all properties start out as nil in Objective-C and that sending a message to nil does nothing, therefore you must initialize using [[Class alloc] init]; before sending a message to a newly created property. However, what about if I'm not sending messages to this property or if I set the property using self.property = something? Do I need to alloc init in these cases as well? Also, do UI properties start out as nil as well, such as a UILabel property