iDevGames Forums
Using structs in Objective-C - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Using structs in Objective-C (/thread-1797.html)



Using structs in Objective-C - demonpants - Feb 5, 2009 01:40 PM

I'm not sure the exact right syntax for using structs in Objective-C. Basically I want to convert some of the things I was using objects for to structs instead, like vectors and path-finding nodes. This is for the sake of saving some processor.

I want this to be converted to a struct:
Code:
@interface PathData : NSObject
{
@private
    LevelSpace *space;
    unsigned int cost;
    PathData *parent;
}

- (id) initPathData:(LevelSpace *)obj withCost:(unsigned int)cst withParent:(PathData *)rent;
+ (PathData *) createPathData:(LevelSpace *)obj withCost:(unsigned int)cst withParent:(PathData *)rent;

@property (retain) LevelSpace *space;
@property (retain) PathData *parent;
@property unsigned int cost;

@end

Essentially I want to be able to have a static constructor that will return a struct that is initialized correctly, and that's about it. So pretty much I want:

Code:
@interface PathData : NSObject
{
    struct pathData
    {
        LevelSpace *space;
        unsigned int cost;
        struct pathData *parent;
    }data;
}

+ (struct pathData *) createPathData:(LevelSpace *)obj withCost:(unsigned int)cst withParent:(PathData *)rent;

@end

Then within a different class I want to say:
Code:
struct levelSpace space =  [PathData createPathData:obj withCost:0 withParent:other];
levelSpace.cost++;
levelSpace.parent = nil;
//etc.

Just a couple quick corrections (or affirmations) of my code would be great. Thanks a lot. My syntax above came from the very sparse examples I've been able to find on Google.

Also, while I'm here, what sort of speed increases will I be seeing with this? I've been using objects for path finding, so probably about an average of 100-1,000 are being instantiated every second, then promptly being deallocated. In addition, how does Objective-C handle deallocating these as well as other primitives? Only NSObject can have retain, autorelease, etc. applied to it, but I don't really see any options for other data types. Is there some sort of garbage collector that handles them, or do they just float around perpetually?

Thanks a lot.


Using structs in Objective-C - longjumper - Feb 5, 2009 02:13 PM

No need to use Objective-C at all if that is the case.

Use a simple C function that returns a PathData object.


Using structs in Objective-C - demonpants - Feb 5, 2009 03:19 PM

longjumper Wrote:No need to use Objective-C at all if that is the case.

Use a simple C function that returns a PathData object.

How (mostly where) do I incorporate that with Objective-C? I know each of the languages separately, my problem is incorporating them. Can you throw a quick code example up here? Thanks.


Using structs in Objective-C - ThemsAllTook - Feb 5, 2009 03:35 PM

demonpants Wrote:I know each of the languages separately, my problem is incorporating them.

Objective-C is a proper superset of C. Anything you can do in C can be done identically in Objective-C. So, you really don't need to think of them as different languages; Objective-C is simply "C plus some more stuff".

If you need a code example, longjumper was talking about doing something like this:

Code:
// In a header file somewhere
struct pathData {
    LevelSpace * space;
    unsigned int cost;
    struct pathData * parent;
};

struct pathData createPathData(LevelSpace * space, unsigned int cost, struct pathData * parent);

// In an implementation file somewhere
struct pathData createPathData(LevelSpace * space, unsigned int cost, struct pathData * parent) {
    struct pathData result;
    result.space = space;
    result.cost = cost;
    result.parent = parent;
    return result;
}

Then, any C or Objective-C source file that includes that header can access the pathData struct and the createPathData function. There aren't any additional complications introduced by Objective-C.


Using structs in Objective-C - demonpants - Feb 5, 2009 04:05 PM

Great, that works just fine. Thanks for clarifying that for me.

Can anyone answer these questions for me?

Quote:Also, while I'm here, what sort of speed increases will I be seeing with this? I've been using objects for path finding, so probably about an average of 100-1,000 are being instantiated every second, then promptly being deallocated. In addition, how does Objective-C handle deallocating these as well as other primitives? Only NSObject can have retain, autorelease, etc. applied to it, but I don't really see any options for other data types. Is there some sort of garbage collector that handles them, or do they just float around perpetually?

Mostly the question about how memory management should work with structs is important. I can obviously observe the speed increase for myself. Smile The docs say that because I'm not using any of the specific keywords (like retain, alloc, etc.) so I don't need to worry about it, but I am curious how these primitives (and potentially object pointers that are within structs) coincide with autorelease pools. But will I notice my structs getting suddenly being null or will they eat up memory if I don't deallocate them?

Thanks again.


Using structs in Objective-C - TomorrowPlusX - Feb 5, 2009 04:12 PM

If you're using ThemsAllTook's code, the structs are on the stack, and you don't need to worry about freeing them. If however you ever malloc() one of these structs, you'll have to free() it when you're done with it.


Using structs in Objective-C - demonpants - Feb 5, 2009 04:24 PM

TomorrowPlusX Wrote:If you're using ThemsAllTook's code, the structs are on the stack, and you don't need to worry about freeing them. If however you ever malloc() one of these structs, you'll have to free() it when you're done with it.

Great, thanks.

Also, any ideas on how I can get a struct into one of Apple's collections? Basically I want to add them into an NSMutableArray. Are my only two options to wrap it in an object (defeating the purpose) or to make my own C mutable array class?


Using structs in Objective-C - SethWillits - Feb 5, 2009 05:42 PM

[NSValue valueWithPointer:]


Using structs in Objective-C - demonpants - Feb 5, 2009 06:31 PM

FreakSoftware Wrote:[NSValue valueWithPointer:]

Yeah, I saw that, but that's just a wrapper basically. Anyway I've got it all working now, so thanks for all the help.


Using structs in Objective-C - SethWillits - Feb 6, 2009 01:53 PM

Isn't that what you asked for? Obj-C collections only work with objects, so you'll need to wrap your struct in an object one way or another.


Using structs in Objective-C - longjumper - Feb 6, 2009 02:57 PM

Seth is correct, you need to wrap your object in a Objective-C object. valueWithPointer: won't work unless you explicitly allocated memory for that structure. If you are just throwing a structure around on the stack, you might want to use NSData's dataWithBytes:length:.