alloc

Objective-C中new与alloc/init的区别

僤鯓⒐⒋嵵緔 提交于 2020-02-28 17:20:37
在实际开发中很少会用到new,一般创建对象我们看到的全是[[className alloc] init],但是并不意味着你不会接触到new,在一些代码中还是会看到[className new],还有去面试的时候,也很可能被问到这个问题。 那么,它们两者之间到底有什么区别呢?我们先来看源码: + new { id newObject = (*_alloc)((Class)self, 0); Class metaClass = self->isa; if (class_getVersion(metaClass) > 1) return [newObject init]; else return newObject; } 而 alloc/init 像这样 + alloc { return (*_zoneAlloc)((Class)self, 0, malloc_default_zone()); } - init { return self; } 通过源码中我们发现,[className new]基本等同于[[className alloc] init], 区别只在于alloc分配内存的时候使用了zone。 那么,这个zone是个什么东西呢? 它是给对象分配内存的时候,把关联的对象分配到一个相邻的内存区域内,以便于调用时消耗很少的代价,提升了程序处理速度。 为什么不推荐使用new?

What objects do I alloc/release

偶尔善良 提交于 2020-01-07 07:08:07
问题 in Objective-C and simply speaking - am I correct when assuming that all pointer variables must be released when they're not used any more? Every pointer variable (*) is a class of some kind, or not? Every pointer variable therefore needs to be allocated and initialised using "alloc" and "init" (or similar)? When declaring variales using Object methods I may not need "alloc" or "init"? Number declarations (BOOL, int, float, etc) do not require memory management as long as they're not declared

Initialize and allocate multiple variables/objects | Objective C

ⅰ亾dé卋堺 提交于 2020-01-06 19:42:06
问题 How would I turn this: xmlObject *system_domain = [xmlObject alloc] xmlObject *system_description = [xmlObject alloc] xmlObject *system_type = [xmlObject alloc] Into something where I would only have to write xmlObject and [xmlObject alloc] once. xmlObject *system_domain, *system_description, *system_type = [xmlObject alloc] This makes all of them xmlObjects, but does not allocate each one. Any help would be much appreciated. 回答1: If you've got a set of related objects, and being able to

Are objects in Objective-C ever created on the stack?

别来无恙 提交于 2020-01-05 03:18:07
问题 As far as I understand, in C++ you can create objects on the stack: SomeClass object = SomeClass(); or on the heap: SomeClass *object = new SomeClass(); In Objective-C you always seem to create objects on the heap, as [SomeClass alloc] returns a pointer to a new instance. Is this correct? Are objects ever allocated on the stack? If so, what would be a good example? Else, why not? 回答1: The short answer is that objects are always allocated on the heap, not on the stack. This isn't quite the

Using alloc, init in ARC enabled projects

删除回忆录丶 提交于 2020-01-02 03:28:19
问题 Actually I am working on a project with ARC enabled. I know using alloc and init is taking ownership of the object. I know, If I create a string like this NSString *myString = [[NSString alloc]initWithFormat:@"Something"]; then I need to release the myString on myself. What If I am using ARC enabled? I cannot release on myself. So will it create a leak? Or should I don't create object like this? I can create a string like below code too. NSString *myString = [NSString stringWithFormat:@

ObjC-Why it is incorrect when implement [alloc] and [init] methods separatly? [duplicate]

我的梦境 提交于 2019-12-31 06:17:47
问题 This question already has answers here : Why should I not separate alloc and init? (4 answers) Closed 4 years ago . Never initialize an object without reassigning any pointer to that object. As an example, don’t do this: NSObject *someObject = [NSObject alloc]; [someObject init]; If the call to init returns some other object, you’ll be left with a pointer to the object that was originally allocated but never initialized. Actually, this is a example in Apple's ObjC document, but i'm not quite

why can't I use partial struct initialization for a malloced struct in C

自闭症网瘾萝莉.ら 提交于 2019-12-24 17:17:35
问题 Apperently in C99 you can simply initialize a statically allocated struct in this way struct sometype { int a; double b; }; sometype a = { .a = 0; }; Well, this does not apply to a struct on heap like this. struct sometype *a = malloc(sizeof(struct sometype)); *a = { .a = 0; }; With GCC 4.9.2, the compiler complained error: expected expression before '{' token I know this is silly, but is there any syntax or technical reason that I cannot do this? 回答1: There is a difference between struct

C, realloc that fails if the allocation cannot grow IN PLACE

一曲冷凌霜 提交于 2019-12-24 00:55:20
问题 Is there a way to grow an array in C, but only if the memory can be grown in place (That is, fail to grow if the pointer needs to be changed)? 回答1: In standard C there is no function capable of doing that. 来源: https://stackoverflow.com/questions/21630787/c-realloc-that-fails-if-the-allocation-cannot-grow-in-place

Allocating memory in Erlang C NIF

匆匆过客 提交于 2019-12-22 23:12:22
问题 Why would one use void *enif_alloc_resource(ErlNifResourceType* type, unsigned size) as opposed to void *enif_alloc(size_t size) when trying to allocate memory from an Erlang C NIF? Reference does not specify much as to why. http://www.erlang.org/doc/man/erl_nif.html#enif_alloc 回答1: enif_alloc_resource is used to create resources which are garbage collected by the vm when not used any more. enif_alloc works just like malloc, only is uses an Erlang VM specific implementation rather than the

Is a malloc() needed before a realloc()?

♀尐吖头ヾ 提交于 2019-12-18 12:08:32
问题 Since I had read realloc will act as malloc if the size pointed is 0, I was using it without malloc(), provided the pointer was static, global, or explicitly set to NULL if automatic. However, I notice a lot of programmers try to set it or set it to malloc(1). Is it needed? 回答1: From Open Groups' specifications: If ptr is a null pointer, realloc() shall be equivalent to malloc() for the specified size. If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if