做一件指针设置为一个对象零影响对象或其他指针呢? [英] Does setting one pointer to an object to nil affect the object or other pointers to it?

查看:156
本文介绍了做一件指针设置为一个对象零影响对象或其他指针呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道深拷贝和浅复制,但我想在这里问什么之间的主要区别是什么实用。

Although I know the main difference between Deep Copy and Shallow Copy but what I want to ask here is something practical.

NSArray *firstArray = [NSArray arrayWithObjects:@"first", @"second",@"third",  nil];
NSArray *secondArray    = firstArray;
secondArray             = nil;

NSLog(@"First Array : %@",firstArray);
NSLog(@"Secon Array : %@",secondArray);

无论是 firstArray secondArray 有通过把断点所见相同的参考。

Both the firstArray and secondArray has the same reference as seen by putting break point.

现在我的问题是这样的,如果两者都具有相同的参考那么为什么 firstArray 不改变,如果在 secondArray 改变。

Now my question is this, if both have same REFERENCE then why the firstArray does not change if the secondArray is changed.

我真的很困惑在这里...
任何一个可以请让我清楚。

I am really confused here... Can any one please make me clear.

谢谢期待。

推荐答案

我需要使用ASCII艺术来解释这更好的...

I need to use ASCII art to explain this better...

firstArray
           \
            \
             NSArray - the actual data
            /
           /
secondArray

firstArray secondArray 只是指针,抱着相同的数字(虚拟)内存地址所在的的NSArray 数据存储。如果指针之一被设置为指向,或为此事其他一些数据,也不会引发在的NSArray任何改变还是其他指针。

firstArray and secondArray are just pointers, holding the same numerical (virtual) memory address where the NSArray data is stored. If one of the pointers is set to point to nil, or to some other data for that matter, it won't trigger any change in the NSArray nor the other pointer.

需特别注意的是,虽然 firstArray secondArray 在最初持有相同的价值,他们在不同的存储不同的变量地址。你可以看到,如果打印:

Note especially that while firstArray and secondArray both held the same value initially, they are distinct variables at different memory addresses. You can see that if you print:

NSLog(@"First Array : %@",&firstArray);
NSLog(@"Secon Array : %@",&secondArray);

我觉得你是为他们的地址弄错便指着地址(的NSArray 数据的地址),然后结束他们是同一个变量不同的名称。他们不是!每个人都有不同的记忆。

I think you were mistaking the address they pointed to (the NSArray data's address) for their address, and then concluding they were different names for the same variable. They're not! Each has different memory.

您可能想在指针念起来:<一href=\"http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean/4955297#4955297\">What的确&QUOT;非关联化&QUOT;一个指针是什么意思?

You might want to read up on pointers: What does "dereferencing" a pointer mean?

一个指针变量是很像一个整数变量 - 它需要的内存几个字节(无论在其可以被指向的对象的大小的),并存储数字。该号码可以用于记录(虚拟)存储器地址(多少从0原点的(虚拟)存储器字节)在其中一个特定的对象指向的类型可能被发现。所以,这是一个有点像有一张纸,上面我们可以写一个房子的地址。现在,指针不是房子(数据对象),你可以删除原来的地址,写一些其他的房子任何时候,你喜欢的地址,并且可以使用该地址找到房子。这就是发生在这个问题......你有效有两个上你写你的家的地址纸片,然后再删除其中之一,并且预计随着其他戏剧性的影响等一张纸被擦除和/或被破坏的房子/对象。指针不工作这样的。

A pointer variable is much like an integer variable - it takes a few bytes of memory (regardless of the size of the objects at which it may be pointed) and stores a number. That number can be used to record the (virtual) memory address (how many bytes from the 0 origin of (virtual) memory) at which a particular object of the pointed-to type may be found. So, it's a bit like having a piece of paper on which we can write the address of a house. Now, the pointer is NOT the house (data-object), and you can erase the original address and write the address of some other house anytime you like, and you can use that address to find the house. This is what happened in this question... you effectively had two "pieces of paper" on which you'd "written" the address of your "house", then erased one of them, and expected a dramatic affect such as the other piece of paper being erased and/or the house/object being destroyed. Pointers don't work like that.

有些语言有一个独特的机制 - 这被称为引用,别名,链接和各种语言的术语许多其他的事情 - 这就像一个额外的名或别名的对象。任何你做来的这些东西是透明的,以该对象来代替。回到我们的房子比喻:这就像有不同的名称为您的家 - 而不是使用街道地址,你可以称之为家,或我的城堡,如果你给的指示烧家你的房子会真正燃烧。用指针到你家 - 他说:烧指针到户,除非你去一点点额外的努力,解引用指针,房子只是操作灼伤一张纸

Some languages have a distinct mechanism - which has been called "references", "aliases", "links" and many other things in various languages' terminology - that works like an additional name or alias for an object. Anything you "do" to one of these things is transparently done to that object instead. Returning to our house analogy: this is like having different names for your house - instead of using the street address, you may call it "home", or "my castle", and if you give the instruction "burn home" your house will really burn. With a pointer to your home - saying "burn pointer-to-home" just burns the piece of paper unless you go to a little extra effort to "dereference" the pointer and operate on the house.

当他们需要在运行时程序传来传去,他们参考/别名/东西......通常像指针来实现 - 即存储数据对象的地址有效的变量。但是,无论他们已经在运行时已经通过周围或只在编译器的内部记录短暂存在过,他们经营如上所述。

When they need to be passed around the program at run-time, they reference/aliases/things... are typically implemented like pointers - effectively variables that store the address of the data object. But, regardless of whether they've been passed around at run-time or only existed transiently in the compiler's internal records, they operate as described above.

所以,从背景,其中参考,是用来形容这些参考/别名/任何事物的技术术语的人,这是错误的听别人说的指针引用......我们的反应是没有他们'再没有 - 你会燃烧,而不是纸上的房子,如果你这样想。但是,有程序员对他们来说,引用的整个社区为指针的另一种说法,而这些其他的事情可能会被称为别名或链接或假名,或天知道什么,或者他们可能只是不存在于这种语言/社区(如C程序员)。

So, to people from a background where "reference" is the technical term used to describe these reference/alias/whatever things, it's "wrong" to hear people say pointers are references... our reaction is "no they're not - you'll burn the paper instead of the house if you think like that". But, there are whole communities of programmers for whom "reference" is another term for "pointer", and these other things might be known as "aliases" or "links" or "pseudonyms" or goodness knows what, or they might just not exist in that language/community (e.g. for C programmers).

我们所有的注释中讨论只是这种潜在的混乱。

All our discussion in comments is just about this potential confusion.

重要的,这是您了解,在内存上的指针操作会影响数字地址指针店,而不是指向的对象 除非你解引用该指针,而不是其他指针到该对象(除非他们所有的智能指针有一些故意策划的同步)。

The important this is that you understand that operating on a pointer affects the numeric address in memory that pointer stores, and not the pointed-to object unless you dereference that pointer, and not other pointers to that object (unless they're all "smart pointers" with some deliberately orchestrated synchronisation).

我建议你阅读上面,直到你不能得到更多的出来,然后重新读<一个href=\"http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean/4955297#4955297\">What的确&QUOT;非关联化&QUOT;一个指针是什么意思?再重读此等等,直到你高兴你已经掌握了这一切。

I suggest you read the above until you can't get any more out of it, then re-read What does "dereferencing" a pointer mean? then reread this etc. until you're happy you've grasped it all.

在下面的评论,你说:

截至我的理解,指针是可以在它包含一个内存地址和内存地址总是作为参考意义的一个值的变量。

Up to my understanding, Pointer is a variable that can contain a memory address in it and memory address is always takes as in sense of reference to a value.

在指针的存储器地址可在点合法对象/值引用/,或者它可以未初始化或垃圾,只是作为一个整数变量可以具有有用的数目在它或将未初始化(尚未设置),或者说你不再有兴趣在早期处理后举行一些遗留的价值。

The memory address in the pointer can reference/point-at a legitimate object/value, or it may be uninitialised or garbage, just as an integer variable may have a useful number in it or be uninitialised (not yet set), or hold some left-over value after earlier processing that you're no longer interested in.

让我在这里明确我的观点,指针有一个内存地址,在这个内存地址会出现一定的价值。

Let me clear my point here, A Pointer has a memory address , at that memory address there will be some value.

这是不明确的,但正确的,但是你是认真的。歧义:指针具有在指针本身被存储在存储器的意义上的存储器地址。在这个意义上说,每个变量具有存储器地址。但是,指针也指向的数据的存储器地址的读出有一个存储器地址。 (这是我在上面链接对方的回答解释。)

That's ambiguous but correct however you mean it. The ambiguity: the pointer has a memory address in the sense of the memory where the pointer itself is stored. In that sense, every variable has a memory address. But, pointers also have a memory address in the sense of the memory address of the pointed-to data. (This is explained in my other answer linked above.)

因此​​,如果一些人有得到那个值,就必须有到达那里的基准而且参考]是包含认为点的属性

So if some one has to get that value, there must be a reference to reach there and that reference is the property of that Point that is containing it.

好吧,我认为值,你指的是poined-价值:​​你必须有一个内存地址来到达那里(不管你要调用该地址的引用如上所述),以及该地址的的存储在指针,所以你可以认为这是一个指针的财产,如果你在某些语言所使用的意义上使用财产。所以,是的! :-)我认为你的理解是可能是正确的,但你的术语还是有点IM precise和模棱两可。

Well, I think by value you mean the poined-to value: you must have a "memory address" to reach there (whether you want to call that address a reference is discussed above), and that address is stored in the pointer, so you could consider it a "property" of the pointer if you use "property" in the sense used by some languages. So yes! :-) I think your understanding's probably correct, but your terminology is still a little imprecise and ambiguous.

这篇关于做一件指针设置为一个对象零影响对象或其他指针呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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