Using structs in Objective-C

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.

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?

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:

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. 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?

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.

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?

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:.