为什么从用户输入中获取的两个NSString最终具有相同的地址? [英] Why do two NSStrings taken from user input end up with the same address?

查看:83
本文介绍了为什么从用户输入中获取的两个NSString最终具有相同的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道操作系统或编译器如何管理内存使用情况.以登录屏幕为例.

I am curious to know how the OS or compiler manages the memory usage. Take this example of a login screen.

如果我为两个用户ID和输入相同的字符串,密码:两次都说"anoop",以下两个字符串具有相同的地址:

If I enter the same strings for both user ID & password: say "anoop" both times, the following two strings have same addresses:

NSString *userID = self.userNameField.stringValue;
NSString *password = self.passwordField.stringValue;

如果我输入"anoop",地址分别为"Anoop".

If I enter "anoop" & "Anoop" respectively, the address changes.

编译器如何知道密码文本与用户ID相同,因此它使用相同的引用而不是分配新的内存空间?

How does the compiler know that the the password text is same as the user ID, so that instead of allocating a new memory space it uses the same reference?

推荐答案

在这种情况下,答案是您正在带有Objective-C标签的指针的64位平台上进行测试.在这个系统中,某些指针"根本不是存储器地址;他们直接在指针值中编码数据.

The answer in this case is that you are testing on a 64-bit platform with Objective-C tagged pointers. In this system, some "pointers" are not memory addresses at all; they encode data directly in the pointer value instead.

这些系统上的一种带标记的指针完全在指针中编码短字符串,而"anoop"是可以这种方式编码的字符串.

One kind of tagged pointer on these systems encodes short strings of common characters entirely in the pointer, and "anoop" is a string that can be encoded this way.

查看星期五问答,2015- 07-31:标记指针字符串进行了非常详细的说明.

Check out Friday Q&A 2015-07-31: Tagged Pointer Strings for a very detailed explanation.

这篇关于为什么从用户输入中获取的两个NSString最终具有相同的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆