Re: Declaring multidimensional arrays in obj-c
Re: Declaring multidimensional arrays in obj-c
- Subject: Re: Declaring multidimensional arrays in obj-c
- From: "Ken Ferry" <email@hidden>
- Date: Wed, 3 Sep 2008 00:20:55 -0700
On Tue, Sep 2, 2008 at 2:35 PM, Don Arnel <email@hidden> wrote:
> Thanks, everyone. I guess I'm going the pointer/malloc way.
This is surely the right thing to do. However, it _is_ possible to
allocate a dynamic amount of space inline with your object, it just
doesn't play well with anyone else doing the same. Check out the
second argument of NSAllocateObject.
"""
Creates and returns a new instance of a given class.
id NSAllocateObject (
Class aClass,
NSUInteger extraBytes,
NSZone *zone
);
Parameters
aClass
The class of which to create an instance.
extraBytes
The number of extra bytes required for indexed instance variables
(this value is typically 0).
zone
The zone in which to create the new instance (pass NULL to specify the
default zone).
"""
If you pass something larger than zero for extraBytes, a larger than
normal chunk of memory will be malloc'd for the object, and you get
some extra space off past all the normally defined instance variables.
You can retrieve a pointer to this memory with the runtime function
object_getIndexedIvars.
It's probably only a good idea to use this with direct subclasses of
NSObject, if at all. A few Cocoa classes do actually make use of this
trick, and if you and someone else both think they own the space, bad
things will happen.
-Ken
On Tue, Sep 2, 2008 at 2:35 PM, Don Arnel <email@hidden> wrote:
> Thanks, everyone. I guess I'm going the pointer/malloc way.
>
> On Sep 2, 2008, at 5:27 PM, Keary Suska wrote:
>
>> 9/2/08 2:57 PM, also sprach email@hidden:
>>
>>> I don't see the problem...
>>> Just use a pointer to a pointer to a pointer to a ...
>>> Every array in C is just a hidden pointer and if you try to
>>> dynamically allocate an array in C then you are left only with one
>>> option: malloc, calloc, realloc
>>
>> No one has suggested that there aren't functional equivalents, simply that
>> it is not possible to declare a variable length array as an object ivar,
>> which is what the OP was (directly) asking.
>>
>>> On 02 Sep 2008, at 22:49, Keary Suska wrote:
>>>
>>>> 9/2/08 2:25 PM, also sprach email@hidden:
>>>>
>>>>> I'm having a devil of a time trying to figure out what seems to be a
>>>>> pretty basic scenario: declaring a multidimensional array in the
>>>>> interface section of a class when the array dimensions are unknown
>>>>> until runtime. Can anyone point me in the right direction with this
>>>>> one?
>>>>
>>>> If you mean C arrays, AFAIK you can't. C99 spec'ed variable length
>>>> arrays
>>>> (VLAs) don't work that way. It will work out, IIRC, that you can only
>>>> use
>>>> them as stack variables. And not static ones either. I believe it's
>>>> because
>>>> of the initialization rule for VLAs.
>>
>>
>> Keary Suska
>> Esoteritech, Inc.
>> "Demystifying technology for your home or business"
>>
>>
>> _______________________________________________
>>
>> 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
>
> _______________________________________________
>
> 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
>
_______________________________________________
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