Re: Newbie Question: implementing a stack
Re: Newbie Question: implementing a stack
- Subject: Re: Newbie Question: implementing a stack
- From: "Michael Ash" <email@hidden>
- Date: Sat, 20 Dec 2008 21:19:32 -0500
On Sat, Dec 20, 2008 at 6:52 PM, Steve Wetzel <email@hidden> wrote:
> I do see your point Graham but what I am trying to understand is how to work
> with the stack if I don't copy the object to put on it. If I simply push
> the pointer on the stack, it seems that I have to make a lot of objects in
> the code that handles the stack object. If I have an class MyObject I wish
> to push on the stack:
>
> MyObject *myObj1 = [[MyObject alloc] init];
> MyObject *myObj2 = [[MyObject alloc] init];
> ...
> MyObject *myObj10 = [[MyObject alloc] init];
>
>
> [stack push:myObj1];
> [stack push:myObj2];
> ...
> [stack push:myObj10];
>
> Each time I want to push a MyObject onto the stack I need to create a new
> MyObject. If I copy I can do:
>
> MyObject *myObj = [[MyObject alloc] init];
>
> [stack push:myObj];
> <assign new attributes to myObj>
> [stack push:myObj];
> <assign new attributes to myObj>
> ...
>
>
> I guess can simply assign the pointer, but if I do, it seems to me I will
> need an NSMutableArray to hold myObj1... myObj10 (or more). If I do that,
> what benefit is the stack?
You're very confused. When you assign something new to myObj, you're
only affecting that one pointer. You don't affect anything else that
has a reference to the original object. For example:
NSString *str = @"hello";
[stack push:str];
str = @"world";
[stack push:str];
Your stack now contains @"hello" and @"world". It does not matter what
its copying behavior is, this is *always* true. (Try it with an
NSMutableArray and see; NSMutableArray does not copy the objects it
receives.) When you write str = @"world" you just put a new address
into the str pointer. But the stack isn't holding your str variable.
It's simply holding the value that str held originally. So there's no
problem.
What copying saves you from is stuff like this:
NSMutableString *str = [NSMutableString stringWithString:@"hello"];
[stack push:str];
[str setString:@"world"];
[stack push:str];
In this case the stack ends up holding two copies of a single mutable
string which contains the text @"world". Here we aren't mutating
"str", we're mutating the *object* that it points to. If the stack
copied the object then we wouldn't mutate the copy and so the
-setString: call wouldn't change its contents.
This usually isn't much of a problem. And to the extent that it is,
being able to push non-copyable objects or referenced mutable objects
onto a stack will generally far outweigh them. Follow Cocoa's lead:
with the exception of dictionary keys, Cocoa does not copy objects
that are put into collections.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden