Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Two Questions

Two Questions
Thread Tools
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Wisconsin
Status: Offline
Reply With Quote
Jun 15, 2002, 11:30 AM
 
How do you do dynamic data structures in ObjC? I tried making a linked list class like I would in C++ using structures, new and delete. I never actually tried to run anything because I was pretty sure this wouldn't work.

When you do something like this:
[array addObject:@"String"];
What is actually happening? Is a new NSString or NSMutableString created?
Is this equivalent to this?
NSString * string = [[NSString alloc] initWithString:@"String"];
[array addObject:string];

Thanks for any help.
     
Junior Member
Join Date: Jul 2001
Location: Mos Eisley Cantina
Status: Offline
Reply With Quote
Jun 15, 2002, 12:46 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">NSString * string = [[NSString alloc] initWithString:@&quot;String&quot;];
[array addObject:string];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">In this case, when you initially allocate the NSString, it has a retain count of one. Adding it to the NSArray increases its retain count to two. After it is removed from the array (or the array is destroyed), you have to release it (or set it to autorelease) for it to go away.

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[array addObject:@&quot;String&quot;];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Here, I think [NSString stringWithString:@"String"] is probably called. This is different because after the stringWithString call, the object is on autorelease (when your thread blocks, awaiting for messages for its Run Loop; your object might go away). Once you add it to the array, its retain count goes to one. After it is removed from the array, you don't need to explicitly release it, since its retain count is decremented and it will be released for you.

Have a look at memory management in Cocoa on Apple's website which will explain all of this in more detail.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 15, 2002, 07:45 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by seppak:
<strong>How do you do dynamic data structures in ObjC? I tried making a linked list class like I would in C++ using structures, new and delete. I never actually tried to run anything because I was pretty sure this wouldn't work.

When you do something like this:
[array addObject:@"String"];
What is actually happening? Is a new NSString or NSMutableString created?
Is this equivalent to this?
NSString * string = [[NSString alloc] initWithString:@"String"];
[array addObject:string];

Thanks for any help.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">A linked list class should work in Obj-C, I think. I can't think of any reason why it wouldn't. It probably wouldn't look exactly the same as in C++, but the linked list concept wouldn't be too hard to implement.
But you're right that an NSArray is a much easier way of getting the same functionality. Using a linked list would just be reinventing the wheel.
And as for the @"String" thing--the @"String" is basically just like using "String" in normal C, except this creates an NSConstantString object instead of simply a string constant. So I think it's actually not quite equivalent to any NSString methods, though PipelineStall was right that -stringWithString: is functionally equivalent (i.e. in both cases, you wind up with a string whose only owner is your array).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
seppak  (op)
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Wisconsin
Status: Offline
Reply With Quote
Jun 15, 2002, 08:01 PM
 
For the dynamic data structures...I am trying to make a 2dimensional array class indexed by rows and columns. I want it so you dimension its size when you initialize it. You can therefore have holes in the 2d array. I first made an array representing the rows. Each item in the array would be another array representing the columns. This wouldn't work because you can't have nil in an array. Therefore you would have to add everything sequentially. My other idea was to have the each item in the first array point to a linked list class. Each node in the linked list would have its column position, a pointer to its object, and a pointer to the next node itn the list. This would allow you to have holes in the 2d array. How do you handle data structures and is this the best way to accomplish this?

Also, how do you define structures and enumerations globally?

<small>[ 06-15-2002, 09:03 PM: Message edited by: seppak ]</small>
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 15, 2002, 09:42 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by seppak:
<strong>For the dynamic data structures...I am trying to make a 2dimensional array class indexed by rows and columns. I want it so you dimension its size when you initialize it. You can therefore have holes in the 2d array. I first made an array representing the rows. Each item in the array would be another array representing the columns. This wouldn't work because you can't have nil in an array. Therefore you would have to add everything sequentially. My other idea was to have the each item in the first array point to a linked list class. Each node in the linked list would have its column position, a pointer to its object, and a pointer to the next node itn the list. This would allow you to have holes in the 2d array. How do you handle data structures and is this the best way to accomplish this?

Also, how do you define structures and enumerations globally?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Well, there are a few things you could do here. First of all, the "normal," object-oriented way of using a struct or enum across multiple classes is to put it in a header file that will be included in every class that will be using it. That's the tack I (and Apple) would take with it.

As for using an array of arrays...it is doable, I think. You can fill it with some sort of placeholder object on init--there's no reason it has to be nil filling the "empty" slots. And then when the user sets an item, you can assign whatever you want to that index. That's the solution that immediately occurs to me.

As for a linked list class: The basic concept is the same as in C++. Your class would include a pointer to your first node (which could be a struct or an object--I'd probably go object, just for the convenience). You would then dynamically allocate more nodes--using malloc() if it's a struct, or [[xx alloc] init] if it's an object--and assign them as needed.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
seppak  (op)
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Wisconsin
Status: Offline
Reply With Quote
Jun 15, 2002, 10:54 PM
 
I just went with using the data structures using object instead of structures. It worked fine. Now that I have read your post, I probably should have used my original idea of using an array of arrays and using your default placeholders to fill the empty positions. Thanks for the help.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2