对象和指针的区别

发布于 2024-12-17 01:53:16 字数 357 浏览 0 评论 0原文

正如标题所说,指针和对象有什么区别。

假设我有这段代码..

NSString *red = [[NSString alloc]initWithFormat:@"%@"];

和这段代码..

NSString *blue = [NSString stringWithFormat:@"%@"];

假设它们都是指向对象的指针并且几乎相同,是否正确?如果是这样,我应该如何在脑海中思考物体?

如果答案已经存在,我深表歉意,我确实使用了搜索功能,但我只在 C++ 语言中找到了这样的示例,并且想确定它在 Objective-C 中的情况如何。

as the title says, what is the difference between a pointer and an object.

Say I have this code..

NSString *red = [[NSString alloc]initWithFormat:@"%@"];

and this code..

NSString *blue = [NSString stringWithFormat:@"%@"];

Is it correct to assume that they're both pointers to an object and pretty much the same? And if so, how should I think of objects in my mind ?

I do apologize if the answer exists already, I did use the search function but I've only found examples of this in the C++ language and wanted to make sure how it was in objective-c.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

灯角 2024-12-24 01:53:16

除了 Basile Starynkevitch 和 Bram 的回答之外,

在 Objective C 中,代码行之间的区别是,

NSString *red = [[NSString alloc]initWithFormat:@"%@"];

**上面的代码表示您拥有红色对象,因此您有责任释放它。

NSString *blue = [NSString stringWithFormat:@"%@"];

**您不拥有它,程序深处的其他人将拥有它,并且您不必释放它。

我建议您阅读 Apple 的文档以获取更多信息,这太棒了!专门学习《Objective C编程指南》

祝你好运!

PS : iOS 5 有新功能,内存管理由 iOS 本身完成,现在开发人员可以更有创意,而不是做引用计数的三年级数学:)

In addition to Basile Starynkevitch and Bram's answer,

In objective C the difference between your code line is,

NSString *red = [[NSString alloc]initWithFormat:@"%@"];

**Above code says you own red object so it's your responsibility to release it.

NSString *blue = [NSString stringWithFormat:@"%@"];

**You don't own it someone else in your program deep down inside will own this and you don't have to release this.

I would suggest for more information reading Apple's documentation is GREAT! specially Learning, "Objective C programming guide"

Good luck!

PS : iOS 5 has new feature, memory management is done by iOS itself, Now developer can be more creative instead doing 3 grade mathematics of reference counting :)

时光匆匆的小流年 2024-12-24 01:53:16

指针是一个 C 概念,它们在 C、C++ 和 Objective-C 中是相同的。它们只是一个整数,保存存储对象的内存地址。在您的示例中,这两个消息都动态创建一个 NSString 对象。该对象存储在应用程序内存中的位置由操作系统决定,您并不关心。因此操作系统分配了一些内存并在其中存储了一个 NSString 的实例。然后它会返回一个指向该对象的指针,您稍后可以使用该指针来引用实际对象。

Pointers are a C concept, and they're identical in C, C++, and Objective-C. They are simply an integer that holds the memory address where an object is stored. In your example, both of those messages dynamically create an NSString object. Where that object is stored in your application's memory is up to the OS to decide, and you really don't care. So the OS allocates some memory and stores an instance of NSString in it. It then gives you back a pointer to that object, which you can use to reference the actual object at a later time.

无力看清 2024-12-24 01:53:16

指针包含内存中存储对象的地址。

              Memory address    Object
              --------------    ---------
              0
              1
              2
              3
              4
              ...
pointer ----> 10523             myObject1
              10524
              ...

A pointer contains the address in memory where is stored the object.

              Memory address    Object
              --------------    ---------
              0
              1
              2
              3
              4
              ...
pointer ----> 10523             myObject1
              10524
              ...
爱给你人给你 2024-12-24 01:53:16

对象是类的实例。它会占用内存,并且在使用完毕后应该将其释放。指针是您对该对象的引用。

NSString *red = [[NSString alloc]initWithString:@"red"];

这段代码..

NSString *blue = [NSString stringWithString:@"blue"];

红色和蓝色都是指向不同对象的指针。
这里的重要性区别在于,红色是您拥有的,而蓝色不是

[[NSString alloc]initWithString:@"red"];z

返回一个您拥有的对象,并且必须稍后释放

[NSString stringWithString:@"blue “];
返回一个不属于您的对象,并将在下次自动释放池清空时释放

这些概念Apple 的 Objective-C 编程语言 指南(我向您指出了特定部分,它是一个巨大的文档,但是该部分“对象、类和消息传递”应该对您的问题最有帮助)

An object is an instance of a class. It takes up memory while and should be released when you are finished with it. The pointer is your reference to the object.

NSString *red = [[NSString alloc]initWithString:@"red"];

and this code..

NSString *blue = [NSString stringWithString:@"blue"];

red and blue are both pointers to different objects.
The importance difference here is that red is owned by you and blue is not

[[NSString alloc]initWithString:@"red"];z

returns an object that you own and have to release later

[NSString stringWithString:@"blue"];
returns an object that is not owned by you and will be released the next time the autorelease pool is emptied

These concepts are covered in The Objective-C Programming Language guide by apple (I pointed you to the specific section, its a huge document, but the section 'Objects, Classes, and Messaging' should be the most helpful to your questions)

余厌 2024-12-24 01:53:16

我希望我能让这一点更清楚。

该对象在内存中。指针就像指向对象所在内存的箭头。

将其视为建筑物的方向标志。建筑物是对象,方向是指针。

i hope i can make this abit clearer.

the object is in memory. the pointer is like an arow to the memory where the object is.

see it like a directional sign to a building.. the building is the object, the directions are the pointers.

老街孤人 2024-12-24 01:53:16

(我不是 Objective C 专家,但是)

将对象视为内存区域,指针引用该区域但不是该区域。

粗略的比喻:你的车牌号不是你的车。数字是一个数字(由数字组成,或更抽象地是自然数集合的元素)。汽车是你驾驶的东西。

(I'm not an Objective C expert, but)

Think of objects as memory zones, a pointer refer to the zone but is not that zone.

Gross analogy: your car plate number is not your car. A number is a number (made of digits, or more abstractly an element of the set of naturals). A car is something you drive.

離殇 2024-12-24 01:53:16

也不是 Objective-C 专家。这是我最好的猜测。

这两种类型似乎都是指针。然而,看起来不同之处在于第一个(您使用分配的地方)让您负责关联的内存。

对于第二种类型,如果您实例化该对象,使用它,无论什么,然后它超出范围,操作系统可能会为您清理它。但是,对于第一种类型,您负责将分配的内存释放回操作系统。

我猜测 Objective-C 内置了某种引用计数和内存管理来检测该对象何时不再在任何地方被引用,但重要的是该对象应该持续超出该声明的范围,只要你仍然在某处有参考。

您可能可以通过阅读这篇文章找到很多信息:Objective-C 指针?

“对象”和“指针”的一般定义:这两种类型都是指针。一个是你负责内存,另一个是操作系统为你负责。对象简单地定义为类的实例。指针是该实例的内存地址。

Also not an Objective-C expert. Here's my best guess.

Both of those types seem to be pointers. However, it looks like the difference is that the first (where you are using alloc) puts you in charge of the associated memory.

With the second type, if you instantiate the object, use it, whatever, and then it goes out of scope, the OS will likely clean it up for you. However, with the first type, you are in charge of releasing that allocated memory back to the OS.

I'm guessing that objective-C has some sort of reference counting and memory management built in to detect when that object is no longer being referenced anywhere, but the important part is that that object should persist beyond the scope of that declaration as long as you've still got a reference somewhere.

You can probably find a lot of information by reading this post: Objective-C pointers?

As far as the general definition of "object" and "pointer": Both of those types are pointers. One you are in charge the memory, and the other the OS takes responsibility for you. An object is simply defined as an instance of a class. A pointer is the memory address of that instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文