NSString是存储在堆还是堆栈上,什么是初始化一个的好方法? [英] Are NSStrings stored on the heap or on the stack and what is a good way to initialize one

查看:162
本文介绍了NSString是存储在堆还是堆栈上,什么是初始化一个的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个新问题:

1)考虑这一行:

NSString *myString = [[NSString alloc] initWithString: @"Value"]; 

我学到了两件事,但我想确认一下: 据我了解,分配"消息指示NSString的实例将存储在堆"内存中. 我也了解到原始变量(例如"chars")存储在堆栈"内存中.

There were two things I learned, but I would like confirmation: As I learned, the "alloc" message indicates that the instance of NSString will be stored in the "heap" memory. I understood also that primitive variables such as "chars" are stored in the "stack" memory.

这是否意味着:

  • NSString的实例存储在堆内存中;
  • 并且该对象有一个iVar指针(当调用initWithString方法时)指向原始"chars"的"Value"字符串,该字符串驻留在堆栈存储器中吗? 实际上这是如何工作的?
  • the instance of NSString is stored in the heap memory;
  • AND that this object has an iVar pointer (when the initWithString method was called) to the "Value" string of primitive "chars", which reside in the stack memory? How does this work in actuality?

第二个问题直接相关,这给我造成了个人困境(可能是因为我遗漏了一个要点): 2)您将咨询两种方法中的哪一种,为什么?:

The second question is directly related and causes for me a personal dilemma (probably because I'm missing a point): 2) Which of the two approaches would you consult and why?:

NSString *myString = [[NSString alloc] initWithString: @"Value"]; 
NSString *myString = @"Value"; 

如果我的第一个问题得到确认,则两种方法都应最后"指向存储在堆栈存储器中的字符.因此,我实际上看不到使用第一个选项并为保留计数所困扰的目的.

If my first question is confirmed, both approaches should "in the end" point to chars that are stored in the stack memory. I therefore don't actually see the purpose of using the first option and being bothered with the retain count.

推荐答案

简短答案:在这种情况下,两行的结果相同.直接将字符串常量分配给myString是可以的.

Short answer: In this case, both lines have the same result It's fine to assign the string constant directly to myString.

更长的答案:

的确,在堆上分配了Objective-C对象.但是,原始"值始终存储在堆栈中并不正确.不管局部变量是否为原始变量,它们都存储在堆栈中. (例如,您可以声明一个作为结构的局部变量,它不被视为原始变量.)您可以将原始值存储在堆中,但是在这种情况下,访问它们的唯一方法是通过指针.例如:

It's true that Objective-C objects are allocated on the heap. It's not true, though, that "primitive" values are always stored on the stack. Local variables are stored on the stack, whether they're primitive or not. (You can, for example, declare a local variable that's a struct, which isn't considered primitive.) You can store primitive values on the heap, but the only way to access them in that case is via a pointer. For example:

int *someInt = malloc(sizeof(int));  // allocate a block of memory to hold an int
*someInt = 42;                       // store data in that memory

我们总是使用指针来引用Objective-C对象的原因是对象总是分配在堆上.如果要在堆栈上存储某些内容,则编译器需要知道其大小.在Objective-C中,直到程序实际运行时,对象的大小才知道.

The reason that we always use pointers to refer to Objective-C objects is that objects are always allocated on the heap. If you want to store something on the stack, the compiler needs to know its size. In Objective-C, the size of an object isn't known until the program is actually running.

因此,回到您的字符串.尝试执行以下两行:

So, back to your strings. Try executing the following two lines:

NSString *foo = [[NSString alloc] initWithString:@"foo"];
NSString *bar = @"foo";

如果在第二行之后中断,则会发现foobar包含相同的地址;也就是说,它们指向同一对象.由于NSString对象是不可变的,因此使用常量字符串创建一个对象只会返回指向该常量的指针.

If you break after the second line, you'll find that foo and bar contain the same address; that is, they point to the same object. Because NSString objects are immutable, creating one with a constant string just returns a pointer to that constant.

如果这就是全部,为什么在NSString中甚至还有一个-initWithString:? NSString是类集群",也就是说,NSString是几个不同内部类的公共接口.如果将不是常量的NSString *传递给-initWithString:,则返回的对象可能是与使用常量时不同的类的实例.作为类集群,NSString为您隐藏了许多实现细节,因此您不必担心所有字符串的工作方式,就可以针对不同类型的字符串获得良好的性能.

Why is there even an -initWithString: in NSString if that's all it does? NSString is a "class cluster," which is to say that NSString is the public interface to several different internal classes. If you pass a NSString* that's not a constant into -initWithString:, the object you get back might be an instance of a different class than what you get when you use the constant. As a class cluster, NSString hides a lot of implementation details from you so that you get good performance for different types of strings without having to worry about how it all works.

这篇关于NSString是存储在堆还是堆栈上,什么是初始化一个的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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