oc alloc init dealloc

2019-09-28  本文已影响0人  胡志强

https://juejin.im/post/5817c5d30ce4630058564798

calloc

void *
calloc(size_t num_items, size_t size)
{
    void *retval;
    retval = malloc_zone_calloc(default_zone, num_items, size);
    if (retval == NULL) {
        errno = ENOMEM;
    }
    return retval;
}

void *
malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size)
{
    MALLOC_TRACE(TRACE_calloc | DBG_FUNC_START, (uintptr_t)zone, num_items, size, 0);

    void *ptr;
    if (malloc_check_start && (malloc_check_counter++ >= malloc_check_start)) {
        internal_check();
    }

    ptr = zone->calloc(zone, num_items, size);
    
    if (malloc_logger) {
        malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE | MALLOC_LOG_TYPE_CLEARED, (uintptr_t)zone,
                (uintptr_t)(num_items * size), 0, (uintptr_t)ptr, 0);
    }

    MALLOC_TRACE(TRACE_calloc | DBG_FUNC_END, (uintptr_t)zone, num_items, size, (uintptr_t)ptr);
    return ptr;
}
堆空间分配字节(最大256)(在iOS系统下,分配都是16倍数)
#define NANO_MAX_SIZE           256 /* Buckets sized {16, 32, 48, ..., 256} */

malloc

sizeof()
是在编译期确定的大小,根据的是传入的类型,在运行时已经将变量转为常量

@interface aaa : NSObject
{
    int a;
    int b;
    int c;
    int d;
    
}
@end
@implementation aaa
@end

@interface bbb : aaa
{
    int f;
    int g;
    int h;
    int j;
    
}
@end

@implementation bbb
@end
        NSObject* objc =  [[NSObject alloc] init];
        bbb *bb = [[bbb alloc] init];
        int a = 10;
        NSLog(@"%zd",sizeof(a)); //4
        NSLog(@"%zd",sizeof(objc));//8
        NSLog(@"%zd",sizeof(bb));//8
上一篇 下一篇

猜你喜欢

热点阅读