当前位置: 首页 > 编程日记 > 正文

4 OC 中的内存分配以及内存对齐

目录

一  OC  中的内存分配


一  OC  中的内存分配

student 结构体明明是20?为什么是24个字节,因为结构体会按照本身成员变量最大的内存进行对齐,最大成员变量是8个字节,因此就是8的倍数,24个字节。

class_getInstanceSize 返回的是该类内存对齐之后的成员变量的内存空间,你需要多少,计算多少。

malloc_size  是实际系统分配的内存空间

#import <Foundation/Foundation.h>#import <objc/runtime.h>
#import <malloc/malloc.h>// 模拟 NSObject 的实现
struct NSObject_IMPL
{Class isa;
};// 模拟Student 的实现
struct Student_IMPL
{struct NSObject_IMPL IVARS;// 8int _no;// 4int _age;// 4int _height;// 4
}; // 以上实际是20个字节,由于必须是8的倍数,所以是24个字节@interface Student:NSObject
{@publicint _no;int _age;int _height;
}@end@implementation Student@endint main(int argc, const char * argv[]) {@autoreleasepool {// insert code here...Student *stu1 = [[Student alloc]init];// 24NSLog(@"%zd",sizeof(struct Student_IMPL)); // 24 - 32NSLog(@"%zd -- %zd",class_getInstanceSize([Student class]),malloc_size((__bridge const void *)(stu1)));}return 0;
}

虽然结构体只需要24个字节,可以看出来student 实例对象占用的字节是32个字节

前面也有讲到  allozWithZone 方法调用中

    inline size_t instanceSize(size_t extraBytes) const {if (fastpath(cache.hasFastInstanceSize(extraBytes))) {return cache.fastInstanceSize(extraBytes);}// extraBytes 一般是传递的是0size_t size = alignedInstanceSize() + extraBytes;// CF requires all objects be at least 16 bytes.if (size < 16) size = 16;return size;}id 
_class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone)
{void *bytes;size_t size;// Can't create something for nothingif (!cls) return nil;// Allocate and initializesize = cls->alignedInstanceSize() + extraBytes;// CF requires all objects be at least 16 bytes.if (size < 16) size = 16;if (zone) {bytes = malloc_zone_calloc((malloc_zone_t *)zone, 1, size);} else {// 这个位置传递的就是24,但是实际分配的内存确实32,所以还是要看calloc 底层的实现bytes = calloc(1, size);}return objc_constructInstance(cls, bytes);
}

calloc 的源码实现,去苹果源码开源下载 找到malloc 的源码包文件

、https://opensource.apple.com/tarballs/libmalloc/

在 malloc.c 文件中找到的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)
{void *ptr;size_t alloc_size;if (malloc_check_start && (malloc_check_counter++ >= malloc_check_start)) {internal_check();}if (os_mul_overflow(num_items, size, &alloc_size) || alloc_size > MALLOC_ABSOLUTE_MAX_SIZE){errno = ENOMEM;return NULL;}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);}return ptr;
}

以上说了那么多,这这个源码中有一个 NANO_MAX_SIZE 这个宏定义,堆上分配内存的时候,系统把一块块内存都划分好的

即使你不够16个字节,也给你分配16个字节,这里都是16的倍数。

#define NANO_MAX_SIZE            256 /* Buckets sized {16, 32, 48, 64, 80, 96, 112, ...} */

所以以上结构体只需要24个字节,但是系统就是分配给你32个字节的内存

相关文章:

JDE函数--GetUDC(B函数)

GetUDC使用方式&#xff1a; 转载于:https://www.cnblogs.com/GYoungBean/p/4117965.html

k8s crd构建方法_告诉您正在构建没人想要的东西的8种方法(以及处理方法)

k8s crd构建方法by Geoffrey Bourne杰弗里伯恩(Geoffrey Bourne) 告诉您正在构建没人想要的东西的8种方法(以及处理方法) (8 ways to tell you’re building something nobody wants (and what to do about it)) Building something users want is hard — damn hard. They ar…

iOS开发 - 线程与进程的认识与理解

进程&#xff1a; 进程是指在系统中正在运行的一个应用程序&#xff0c;比如同时打开微信和Xcode&#xff0c;系统会分别启动2个进程;每个进程之间是独立的&#xff0c;每个进程均运行在其专用且受保护的内存空间内;线程&#xff1a; 一个进程要想执行任务&#xff0c;必须得有…

Winform开发中常见界面的DevExpress处理操作

我们在开发Winform程序的时候&#xff0c;需要经常性的对界面的一些控件进行初始化&#xff0c;或者经常简单的封装&#xff0c;以方便我们在界面设计过程中反复使用。本文主要介绍在我的一些项目中经常性的界面处理操作和代码&#xff0c;以便为大家开发的时候提供必要的参考。…

5 OC 中的三种对象

目录 OC 中对象的分类 一 instance 对象 二 类对象 三 元类对象 总结: OC 中对象的分类 instance 对象 类对象 元类对象 一 instance 对象 内存中包含哪些信息 isa 指针 其他成员的变量Student *stu1 [[Student alloc]init]; 以上的stu1 就是实例对象 二 类对象 以…

travis ci_如何使用Travis CI和GitHub进行Web开发工作流程

travis ciby Vijayabharathi Balasubramanian通过Vijayabharathi Balasubramanian 如何使用Travis CI和GitHub进行Web开发工作流程 (How to use Travis CI and GitHub for your web development workflow’s heavy lifting) It’s common to hack together apps on CodePen wh…

android.view.ViewRoot$CalledFromWrongThreadException的解决办法

android 是不允许子线程直接更新UI的&#xff0c;如果一定要在子线程直接更新UI就会出现android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.大概意思就是说 只有原来创建找个视图hierarchy的…

6 OC中 isa 和 superclass 的总结

目录 一 关于isa 和 superclass 的总结 二 为什么基类的metaclass 的superclass 指向的是基类的类 三 isa 的细节问题 总结如下&#xff1a; instance 的isa 指向是classclass 的isa 指向是metaclassmetaclass 的isa指向是基类的imetaclassclass 的superclass 指向的是父类…

opencv下指定文件夹下的图片灰度化(图片的读取与保存)-------简单记录

对于此功能其实很简单&#xff1a;主要是在c方面的字母数字的拼接问题存在一定的问题。C数字字母拼接问题&#xff1a; 1 #include <fstream> 2 #include <string> 3 #include <iostream> 4 #include "highgui.h" 5 #include <cv.h> 6 #…

css菜单缓慢滑动_如何使用HTML,CSS和JavaScript构建滑动菜单栏

css菜单缓慢滑动by Supriya Shashivasan由Supriya Shashivasan 如何使用HTML&#xff0c;CSS和JavaScript构建滑动菜单栏 (How to build a sliding menu bar using HTML, CSS and JavaScript) A menu is what you look for when you land at a website. It has options and gi…

素数环问题---深度搜索遍历

1264: 素数环 时间限制: 1 Sec 内存限制: 128 MB提交: 29 解决: 8[提交][状态][讨论版]题目描述 有一个长度为n的环形序列由1,2,3,...,n组成&#xff0c;环中相邻两个整数和均为素数。你需要找到所有满足条件的环。输入 输入n表示环的长度&#xff08;n<16&#xff09;输出…

android之Notification通知

我们在用手机的时候&#xff0c;如果来了短信&#xff0c;而我们没有点击查看的话&#xff0c;是不是在手机的最上边的状态栏里有一个短信的小图标提示啊&#xff1f;你是不是也想实现这种功能呢&#xff1f;今天的Notification就是解决这个问题的。 package cn.com.chenzheng_…

7 OC 中class 类的结构

目录 一 OC 中class 的结构 https://opensource.apple.com/tarballs/objc4/ 在最新的objc源码中 化繁就简来看的话 是以下结构 struct objc_class : objc_object {objc_class(const objc_class&) delete;objc_class(objc_class&&) delete;void operator(con…

apple id无法创建_我们如何使用Apple的学习框架来创建我们的第一个应用程序

apple id无法创建by Jonata Corra由JonataCorra 我们如何使用Apple的学习框架来创建我们的第一个应用程序 (How we used Apple’s learning framework to create our first app) After one month of work, my team and I finished the first version of Echo, our tracker iOS…

个人作业1:小学四则运算——基于控制台

a.需求分析&#xff1a; 自动生成小学四则运算题目的命令行 “软件”&#xff0c;满足以下需求&#xff1a; 除了整数以外&#xff0c;还要支持真分数的四则运算&#xff0c;真分数的运算&#xff0c;例如&#xff1a;1/6 1/8 7/24运算符为 , −, , 并且要求能处理用户…

getchar返回int类型

#include <stdio.h> /* copy input to output; 2nd version */main(){int c;c getchar();while(c ! EOF){putchar(c);c getchar();}} 直觉告诉我getchar返回值应该是char类型的&#xff0c;这个地方为什么不能用char类型来存储getchar()的返回值呢&#xff1f; 其实文中…

8 iOS中KVO 的本质

前言本质 Automatic key-value observing is implemented using a technique called isa-swizzling 这计划的意思就是 自动的键值观察的实现基于 isa-swizzling 原理 1.KVO是基于runtime机制实现的 2.当某个类的属性对象第一次被观察时&#xff0c;系统就会在运行期动态地创…

完成工作表-使用Google Spreadsheets作为数据后端

by Gilad Dayagi通过吉拉德达亚吉 完成工作表-使用Google Spreadsheets作为数据后端 (Get Sheet Done — using Google Spreadsheets as your data backend) If you want to rapidly prototype your next web apps, try using Google Spreadsheets as your data backend.如果您…

BIEE-CSS样式大全

字体属性&#xff1a;(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到&#xff0c;只要用数值就可以&#xff0c;单位&#xff1a;PX、PD 样式 {font-style: oblique;}(偏斜体) italic;(斜体) normal;(正常) 行高 {line-height: normal;}(正常) 单位&…

基于verilog的FPGA编程经验总结(XILINX ISE工具)

1.用ISE仿真的时候.所用变量一定要初始化. ISE默认初始量为"XXXXX", 而Quarters是默认为"00000"的, 其实实际上, 下到FPGA里后也是默认为0的,只是可以说ISE严谨得令人DT吧.比如说用一个累加器, result ABresult ,必须保证在某一刻A, B, result都为定值时,…

6 OC 中的isa 指针

目录 一 isa 指针 二 类对象中的superclass 一 isa 指针 isa 指针 &#xff0c;OC 中的对象都是有的 如下图所示&#xff0c;实例对象isa 指针指向 类对象&#xff0c;类对象的isa 指针指向 元类对象 二 类对象中的superclass superclass 有什么用呢&#xff1f; 比如说创…

btf-raft共识算法_了解Raft共识算法:学术文章摘要

btf-raft共识算法by Shubheksha通过Shubheksha 了解Raft共识算法&#xff1a;学术文章摘要 (Understanding the Raft consensus algorithm: an academic article summary) This post summarizes the Raft consensus algorithm presented in the paper In Search of An Underst…

iOS asset 中定义颜色,xib中便捷访问

在aseet 中定义一个颜色 这样就可以在xib 中访问颜色了&#xff0c;这样就不用重复的去输入

三种序列化方式性能比较

一下代码比较了二进制序列化、xml序列化、Protobuf序列化的运行时间&#xff0c;可是代码显得十分冗余&#xff0c;是否有大神可以指点一二&#xff0c;万分感谢 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; usi…

mac mini 装UBUNTU后没有WIFI解决办法

1、在终端中运行如下命令&#xff0c;重新安装b43相关的全部驱动和firmware: 复制代码代码如下:sudo apt-get install bcmwl-kernel-source #Broadcom 802.11 Linux STA 无线驱动源sudo apt-get install broadcom-sta-commonsudo apt-get install broadcom-sta-sourcesudo apt-…

区块链c端应用小程序_区块链如何真正起作用? 我建立了一个应用程序向您展示。...

区块链c端应用小程序by Sean Han通过肖恩韩 区块链如何真正起作用&#xff1f; 我建立了一个应用程序向您展示。 (How does blockchain really work? I built an app to show you.) According to Wikipedia, a blockchain is:根据维基百科&#xff0c;一个区块链是&#xff1…

HDU 4913 Least common multiple

/* hdu4913 Least common multiple http://acm.hdu.edu.cn/showproblem.php?pid4913 离散化 线段树 统计逆序数思想 tips: 1、线段树中一定要到处都取模&#xff0c;否则wa。。。 2、lazy是乘积的形式出现&#xff0c;不是加和*/ #include <cstdio> #include <algori…

JS ES6 实用笔记

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 这篇博文我会一直更新。 一.导出导入的两中方式 1.export //demo1.js export const a 6 导入语法为&#xff1a; import {a} form demo1 2.export default //demo2.js export default const b 6 …

extjs editgrid增加一行

Ext.onReady(function(){ /* * EditorGridPanel的工作过程 * 1、用户点击单元格 * 2、单元格按照预设的组件显示单元格的内容并处于编辑状态 * 3、离开单元格的编辑状态 * 4、更新编辑后的内容&#xff0c;出现三角号表示已经被修改过 * 5、程序内部变化&#xff1a;将记录设置…

unity 骨骼击碎_保证击碎$ 100挑战的创新策略

unity 骨骼击碎by Glenn Gonda由Glenn Gonda 保证击碎$ 100挑战的创新策略 (A Creative Strategy Guaranteed to Crush the $100 Challenge) Before I became a software engineer, I made my living as a recording studio engineer. I have a non-traditional background an…