标签: alloc

我们可以覆盖目标C中的alloc和dealloc吗?

我知道很少需要覆盖allocdealloc方法,但如果需要,可以在iPhone编程中使用吗?

overriding objective-c dealloc alloc ios

6
推荐指数
2
解决办法
7140
查看次数

使用allocWithZone创建单例:

BNRItemStore是一个单身人士,我很困惑为什么super allocWithZone:必须被称为而不是简单的旧super alloc.然后覆盖alloc而不是allocWithZone.

#import "BNRItemStore.h"

@implementation BNRItemStore

+(BNRItemStore *)sharedStore {
    static BNRItemStore *sharedStore = nil;

    if (!sharedStore)
        sharedStore = [[super allocWithZone: nil] init];

    return sharedStore;
}

+(id)allocWithZone:(NSZone *)zone {
    return [self sharedStore];
}

@end
Run Code Online (Sandbox Code Playgroud)

singleton objective-c alloc

6
推荐指数
1
解决办法
4300
查看次数

在启用ARC的项目中使用alloc,init

实际上我正在开发一个ARC启用的项目.我知道使用allocinit正在拍摄ownership物体.我知道,如果我创建一个像这样的字符串

NSString *myString = [[NSString alloc]initWithFormat:@"Something"];
Run Code Online (Sandbox Code Playgroud)

然后我需要releasemyString自己.如果我使用ARC,该怎么办?我无法释放自己.它会造成泄漏吗?或者我应该不创建这样的对象?

我也可以像下面的代码一样创建一个字符串.

NSString *myString = [NSString stringWithFormat:@"Something"];
Run Code Online (Sandbox Code Playgroud)

但是我需要使用哪种类型的ARC启用项目?如果我使用第一种类型会发生什么?

objective-c init alloc ios automatic-ref-counting

6
推荐指数
1
解决办法
5173
查看次数

iOS错误:'xxxx'没有可见的@interface声明选择器'alloc'

这是我的TextValidator类:

//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 *)checkString
{
return YES;
}

@end
Run Code Online (Sandbox Code Playgroud)

这是我尝试在ViewController.m中初始化TextValidator类的方法:

//ViewController.h
#import <Foundation/Foundation.h>

@interface SignUpViewController : UIViewController <UITextFieldDelegate>
@end

//ViewController.m
#import "SignUpViewController.h"
#import "TextValidator.h"

@interface SignUpViewController ()

@property TextValidator *myValidator;

@end

@implementation SignUpViewController

- (void)viewDidLoad
{
    [[self.myValidator …
Run Code Online (Sandbox Code Playgroud)

objective-c alloc ios

6
推荐指数
2
解决办法
3万
查看次数

使用 mpz 时在 tcache 2 中检测到双空闲是什么意思?

我使用这个程序来存储一个 mpz 值但是当我添加一个 0 ( 400000000000000000000000000000000000000 而不是 40000000000000000000000000000000000000000000000000000000000000000000000000000000000000

free(): 在 tcache 2 中检测到双空闲

中止(核心转储)

#include <iostream>
#include <gmpxx.h>
#include <vector>
using namespace std;

int main(const int argc, const char * const argv[])
{
char *str= (char*)malloc(sizeof(char)*1024);
mpz_class l;
l=40000000000000000000000000000000000000_mpz;
mpz_set_str(l.get_mpz_t(), str, 10);
cout<<endl<<str;
return 0;
}
Run Code Online (Sandbox Code Playgroud)

是否有可能存储大量数字?

谢谢

c++ memory rsa gmp alloc

6
推荐指数
1
解决办法
5万
查看次数

目标C何时使用alloc以及何时不使用

我正在尝试学习目标C,其中一个我觉得非常奇怪的事情就是何时使用alloc以及何时不使用.以这段代码为例:

NSURL *url =[NSURL URLWithString:@"http://www.apple.com"];
Run Code Online (Sandbox Code Playgroud)

为什么你不必先做这样的事情来先分配它?

UIAlert *alert = [[UIAlertView alloc]]
Run Code Online (Sandbox Code Playgroud)

我确信在目标C中我只缺少一些基本的东西,但奇怪的是我很难找到一个没有张贴的解释.谢谢!!

iphone objective-c alloc

5
推荐指数
1
解决办法
3166
查看次数

如何在非托管内存中分配IntPtr []数组?

要在托管代码中分配内存,我使用:

IntPtr [] params_list_n = new IntPtr [5];
Run Code Online (Sandbox Code Playgroud)

但是对于非托管内存我使用Marshal.AllocHGlobal 而且我不明白在这种情况下如何为数组分配内存.

理想情况下,我想使用函数调用Marshal.GetNativeVariantForObject (o, params_list_n[i]); 对于数组的每个元素.

c# arrays memory-management intptr alloc

5
推荐指数
1
解决办法
6735
查看次数

C++未初始化的类实例数组

我一直在寻找,但无法找到答案.有没有办法告诉new操作员不要调用类构造函数?

MyObject* array = new MyObject[1000];
Run Code Online (Sandbox Code Playgroud)

这将召唤MyObject()一千次!我想自己填充分配的内存,不需要在构造函数中初始化任何信息.使用malloc()不是非常和谐的C++代码imho.

MyObject* array = (MyObject*) malloc(sizeof(MyObject) * 1000);
Run Code Online (Sandbox Code Playgroud)

c++ initialization new-operator alloc

5
推荐指数
1
解决办法
2222
查看次数

Erlang,在函数之间传递一个 nif 对象

C nif code在函数中写了一个and new,它创建了一个堆栈结构enif_alloc_resource并返回它。当我使用 function 时enif_make_resources,它总是<<>>以 erlang返回。

这是我的 C 代码:

#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 = enif_alloc(sizeof(ERL_NIF_TERM) * size);
    return s;
}



static ERL_NIF_TERM new_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int size; …
Run Code Online (Sandbox Code Playgroud)

c erlang alloc erlang-nif

5
推荐指数
0
解决办法
429
查看次数

为什么[[NSError alloc] init]; 在Xcode中抛出错误?

我在Xcode中有以下代码:

NSError *error = [[NSError alloc] init];
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Run Code Online (Sandbox Code Playgroud)

它会在日志中引发跟随错误

[NSError init] called; this results in an invalid NSError instance. It will raise an exception in a future release. Please call errorWithDomain:code:userInfo: or initWithDomain:code:userInfo:. This message shown only once.
Run Code Online (Sandbox Code Playgroud)

也许,你会告诉我答案在日志中,但我不明白如何初始化NSError.

nserror alloc ios

5
推荐指数
1
解决办法
6360
查看次数