Re: Newbie Question: implementing a stack
Re: Newbie Question: implementing a stack
- Subject: Re: Newbie Question: implementing a stack
- From: Steve Wetzel <email@hidden>
- Date: Fri, 19 Dec 2008 22:15:46 -0600
On Dec 19, 2008, at Dec 19:6:31 PM, Ricky Sharp wrote:
On Dec 19, 2008, at 1:22 PM, Steve Wetzel wrote:
I am new to this list and new to mac programming as well. I am
working on implementing a stack as a category of NSMutableArray. I
want this stack to be able to store objects. This is what I have
so far:
//
// Stack.h
// Stack implements a basic stack object in an NSMutableArray
object.
#import <Foundation/Foundation.h>
@interface NSMutableArray (Stack)
-(void)push:(id)obj; //push obj of on the stack
-(id)pop; //pop top item off the stack
-(id)peek; //look at the top item on the stack
-(void)clear; //remove all objects from the stack
-(NSUInteger)size; //return the number of items on the stack
@end
If I were doing this, I would actually create an object (subclass of
NSObject) that would contain an NSMutableArray as an attribute.
That way, your API would be very clean.
What you have above would allow users to call any of the NSArray*
suite of APIs in addition to yours above. Sometimes too many APIs
on an object can lead to issues.
My question involves storing objects on the stack. As I understand
it when I store an object on the stack, I am simply storing a
pointer to the object on the stack. If I create an NSString object
called foo and do the following:
NSString *foo = [[NSString alloc] initWithString:@"foo text"];
[FractionStack push:foo];
I have put a copy of the pointer to foo on the stack. If I now
change foo, the top element on the stack changes also. I do not
want this to happen but I am not sure as to the best approach to
make sure this does not happen.
In this specific case, you cannot change foo since NSString is
immutable. If you find yourself adding (pushing) items that are
immutable, it would be best to make a copy or mutable copy as needed.
Also, I am thinking I likely will need to release these objects
when I pop them and when I clear the stack. Is that correct?
Just read up more on the memory management rules. You can also
decide on object-ownership rules. Typically, the container objects
retain objects added to them. Then release them when removed.
That's all you should be concerned about. It's then up to the
original owner of the object to do the right thing.
Ricky, thank you for the response. I think I agree with your answer
on creating stack object, it makes sense. Also you confirmed my
thoughts about memory management. Regarding memory management - does
it make more sense to copy the object to be pushed from within the
Stack object rather then copying it externally before the push call?
I am thinking that it does because then that object is encapsulated
which is how a stack should really work. If I do this I realize I
will need to implement a copy method within any object that I want to
place on the stack if it does not already have one.
_______________________________________________
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