alloc

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

為{幸葍}努か 提交于 2019-12-17 16:09:21
问题 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

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

▼魔方 西西 提交于 2019-12-17 15:35:42
问题 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? 回答1: std::allocator is the default memory allocator for the standard library containers, and you can substitute your own allocators. This allows you

How to release static Objective-C variables

耗尽温柔 提交于 2019-12-12 07:13:02
问题 The StackOverflow question "using static keyword in objective-c when defining a cached variable" references code from Example 4 of Xcode's TableViewSuite that defines a static NSDateFormatter and calls alloc but never calls release . Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not? 回答1: Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not? It depends. If the variable is initialized

Pointer and malloc in swfit

断了今生、忘了曾经 提交于 2019-12-11 12:29:08
问题 I am trying to convert this into swift. Facing issue at memory allocation logic Byte *p[10000]; p[allocatedMB] = malloc(1048576); memset(p[allocatedMB], 0, 1048576); How to write this in swift? 回答1: You can use malloc from Swift, it returns a "raw pointer": var p: [UnsafeMutableRawPointer?] = Array(repeating: nil, count: 10000) var allocatedMB = 0 p[allocatedMB] = malloc(1048576) memset(p[allocatedMB], 0, 1048576) Alternatively, use UnsafeMutablePointer and its allocate and initialize methods

ObjC: using 'self' in init and/or initWithFrame:

心已入冬 提交于 2019-12-11 09:31:56
问题 I heard from someone that the state of a class is not yet stable inside -(id)init, so using 'self.something' is not recommended inside init, but I have a UIView subclass that need to add some subviews to itself when the class is created, so I have to use [self addSubview: subview] in init, or I have to have another initialisation method and after I create the view using: MyView *myView = [[MyView alloc] initWithFrame:frame]; I need to call that method. Is this correct? Does initWithFrame has

call stack shows SIGBUS, what does that mean

人盡茶涼 提交于 2019-12-11 05:17:59
问题 My call stack shows the following: --- called from signal handler with signal 10 (SIGBUS) --- 001301b8 allocate__t24__default_alloc_template2b0i0Ui (20, 20, 309940, 36, fc55 1a00, 0) + a4 0011dcb8 __nw__Q2t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc _template2b0i0_3RepUiUi (10, 10, 7773e8, 0, 0, 0) + 14 0011dcf8 create__Q2t12basic_string3ZcZt18string_char_traits1ZcZt24__default_all oc_template2b0i0_3RepUi (a, a, 7773e8, a, 0, 0) + 24 0011e0bc replace__t12basic

obj-c NSString and alloc / retain / release

萝らか妹 提交于 2019-12-11 04:52:34
问题 This is probably a question that is more about object alloc/retain/release, but I'll use NSString as an example. I'm aware that I can do: NSString* myString = [[NSString alloc] initWithString:@"Test"]; to essentially allocate and initialize a string referenced by my variable myString which I should later call [myString release] upon. However, if after I do this, I set it to some other string such as: myString = someOtherString; does that essentially create a memory leak because I've

Erlang, pass an nif object between functions

喜欢而已 提交于 2019-12-10 08:42:54
问题 I write a C nif code and in function new , it creates a stack struct with enif_alloc_resource and returns that. when i use function enif_make_resources , it always returns <<>> in erlang. Here is my C code: #include "erl_nif.h" static ErlNifResourceType *MEM_RESOURCE; typedef struct { int n; int ps; int pe; ERL_NIF_TERM *data; } Stack; Stack *new(ErlNifEnv *env, int size) { Stack *s = (Stack*) enif_alloc_resource(MEM_RESOURCE, sizeof(Stack)); s->n = size; s->ps = 0; s->pe = size - 1; s->data

iOS error: No visible @interface for 'xxxx' declares the selector 'alloc'

佐手、 提交于 2019-12-07 03:45:08
问题 Here is my TextValidator class: //TextValidator.h #import <Foundation/Foundation.h> @interface TextValidator : NSObject - (BOOL) isValidPassword:(NSString *)checkPassword; - (BOOL) isValidEmail:(NSString *)checkString; - (BOOL) isEmpty:(NSString *)checkString; @end // TextValidator.m #import "TextValidator.h" @implementation TextValidator - (BOOL) isEmpty:(NSString *)checkString { return YES; } - (BOOL) isValidPassword:(NSString *)checkPassword { return YES; } - (BOOL) isValidEmail:(NSString

Allocating memory in Erlang C NIF

给你一囗甜甜゛ 提交于 2019-12-06 05:29:23
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 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 OSs malloc. Take a look at the documentation for ErlNifResourceType and the functions which use it for some