Using structs in Objective-C

Member
Posts: 61
Joined: 2009.01
Post: #1
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.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #2
No need to use Objective-C at all if that is the case.

Use a simple C function that returns a PathData object.
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
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.
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #5
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.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #6
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.
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #7
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?
Quote this message in a reply
⌘-R in Chief
Posts: 1,258
Joined: 2002.05
Post: #8
[NSValue valueWithPointer:]
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #9
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.
Quote this message in a reply
⌘-R in Chief
Posts: 1,258
Joined: 2002.05
Post: #10
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.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #11
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:.
Quote this message in a reply
Post Reply