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.
如果答案已经存在,我很抱歉,我确实使用了搜索功能,但我只是在C ++语言中找到了这个例子,并想确定它在objective-c中的含义。
7 个解决方案
#1
3
In addition to Basile Starynkevitch and Bram's answer,
除了Basile Starynkevitch和Bram的回答,
In objective C the difference between your code line is,
在目标C中,您的代码行之间的区别是,
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"
我建议更多信息阅读Apple的文档是伟大的!特别学习,“Objective C编程指南”
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 :)
PS:iOS 5有新功能,内存管理由iOS本身完成,现在开发人员可以更有创意而不是做3级数学的引用计数:)
#2
2
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.
指针是C概念,它们在C,C ++和Objective-C中是相同的。它们只是一个整数,用于保存存储对象的内存地址。在您的示例中,这两个消息都动态创建NSString对象。该对象存储在您的应用程序中的内存由操作系统决定,您真的不在乎。因此操作系统会分配一些内存并在其中存储NSString的实例。然后它会返回一个指向该对象的指针,您可以使用该指针稍后引用该实际对象。
#3
1
A pointer contains the address in memory where is stored the object.
指针包含存储在对象中的内存中的地址。
Memory address Object
-------------- ---------
0
1
2
3
4
...
pointer ----> 10523 myObject1
10524
...
#4
1
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
[NSString stringWithString:@“blue”];返回不属于您的对象,并在下次清空自动释放池时释放
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)
这些概念在apple的Objective-C编程语言指南中有所介绍(我向您指出了特定的部分,它是一个巨大的文档,但“对象,类和消息”部分应该对您的问题最有帮助)
#5
0
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.
对象在内存中。指针就像对象所在的内存的arow。
see it like a directional sign to a building.. the building is the object, the directions are the pointers.
把它看作是建筑物的指示牌......建筑物是物体,方向是指针。
#6
0
(I'm not an Objective C expert, but)
(我不是Objective C专家,但是)
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.
总比喻:你的车牌数不是你的车。数字是一个数字(由数字组成,或者更抽象地是一组自然元素)。汽车是你驾驶的东西。
#7
0
Also not an Objective-C expert. Here's my best guess.
也不是Objective-C专家。这是我最好的猜测。
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.
这两种类型似乎都是指针。但是,看起来不同的是第一个(你使用alloc的地方)让你负责相关的内存。
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.
我猜这个目标-C内置了某种引用计数和内存管理来检测该对象何时不再被引用,但重要的是该对象应该超出该声明的范围,只要你还有一个参考。
You can probably find a lot of information by reading this post: Objective-C pointers?
你可以通过阅读这篇文章找到很多信息:Objective-C指针?
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.
至于“对象”和“指针”的一般定义:这两种类型都是指针。一个是负责记忆,另一个是OS负责你。对象简单地定义为类的实例。指针是该实例的内存地址。
#1
3
In addition to Basile Starynkevitch and Bram's answer,
除了Basile Starynkevitch和Bram的回答,
In objective C the difference between your code line is,
在目标C中,您的代码行之间的区别是,
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"
我建议更多信息阅读Apple的文档是伟大的!特别学习,“Objective C编程指南”
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 :)
PS:iOS 5有新功能,内存管理由iOS本身完成,现在开发人员可以更有创意而不是做3级数学的引用计数:)
#2
2
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.
指针是C概念,它们在C,C ++和Objective-C中是相同的。它们只是一个整数,用于保存存储对象的内存地址。在您的示例中,这两个消息都动态创建NSString对象。该对象存储在您的应用程序中的内存由操作系统决定,您真的不在乎。因此操作系统会分配一些内存并在其中存储NSString的实例。然后它会返回一个指向该对象的指针,您可以使用该指针稍后引用该实际对象。
#3
1
A pointer contains the address in memory where is stored the object.
指针包含存储在对象中的内存中的地址。
Memory address Object
-------------- ---------
0
1
2
3
4
...
pointer ----> 10523 myObject1
10524
...
#4
1
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
[NSString stringWithString:@“blue”];返回不属于您的对象,并在下次清空自动释放池时释放
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)
这些概念在apple的Objective-C编程语言指南中有所介绍(我向您指出了特定的部分,它是一个巨大的文档,但“对象,类和消息”部分应该对您的问题最有帮助)
#5
0
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.
对象在内存中。指针就像对象所在的内存的arow。
see it like a directional sign to a building.. the building is the object, the directions are the pointers.
把它看作是建筑物的指示牌......建筑物是物体,方向是指针。
#6
0
(I'm not an Objective C expert, but)
(我不是Objective C专家,但是)
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.
总比喻:你的车牌数不是你的车。数字是一个数字(由数字组成,或者更抽象地是一组自然元素)。汽车是你驾驶的东西。
#7
0
Also not an Objective-C expert. Here's my best guess.
也不是Objective-C专家。这是我最好的猜测。
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.
这两种类型似乎都是指针。但是,看起来不同的是第一个(你使用alloc的地方)让你负责相关的内存。
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.
我猜这个目标-C内置了某种引用计数和内存管理来检测该对象何时不再被引用,但重要的是该对象应该超出该声明的范围,只要你还有一个参考。
You can probably find a lot of information by reading this post: Objective-C pointers?
你可以通过阅读这篇文章找到很多信息:Objective-C指针?
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.
至于“对象”和“指针”的一般定义:这两种类型都是指针。一个是负责记忆,另一个是OS负责你。对象简单地定义为类的实例。指针是该实例的内存地址。