Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / CodeWarrior / February 2004



Tip: Looking for answers? Try searching our database.

CFMutableArray to store structs ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jorge - 22 Jan 2004 19:04 GMT
Is it posible to use a CFMutableArray to store structs? ... can
someone show me an example, please?

Thanks in advance!
ROB
Eric VERGNAUD - 22 Jan 2004 22:21 GMT
dans l'article 4beb34a4.0401221104.364ee7df@posting.google.com, Jorge à
rgasparini29@yahoo.com a écrit le 22/01/04 20:04 :

> Is it posible to use a CFMutableArray to store structs? ... can
> someone show me an example, please?
>
> Thanks in advance!
> ROB

It's possible, but it's certainly not a good design. CFArrays and
CFMutableArrays are useful to store objects, that is references, so you
would end up allocating every single struct.

CFMutableData seems more appropriate since it allows you to store arrays of
structs.

Eric
Gregory Dow - 23 Jan 2004 08:18 GMT
> dans l'article 4beb34a4.0401221104.364ee7df@posting.google.com, Jorge à
> rgasparini29@yahoo.com a écrit le 22/01/04 20:04 :
[quoted text clipped - 8 lines]
> CFMutableData seems more appropriate since it allows you to store arrays of
> structs.

CFArrays and CFMutableArray store 4-byte data elements, so they are
meant for storing CF References or other pointer-sized items.

This is true of most of the CF containers. They store 4-byte elements.

Although you could use CFMutableData to store your structs, you would
need to do the indexing yourself. That is, you would need to calculate
the byte offset for the start of each element.

If you are using C++, I suggest using std::vector to store your structs.

-- Greg
Herb Petschauer - 05 Feb 2004 08:26 GMT
> Is it posible to use a CFMutableArray to store structs? ... can
> someone show me an example, please?
>
> Thanks in advance!
> ROB

C++?

SomeStruct        *pSomeStruct;

pSomeStruct = new SomeStruct();
// or use a malloc

// checking for NULL is a good option...
...

::CFArrayAppendValue( someArray, pSomeStruct );

...

SomeStruct        *pSomeOtherStruct;

pSomeOtherStruct = (SomeStruct *)::CFArrayGetValueAtIndex( someArray,
somevalidIndex );

you'll have to iterate the array and deallocate everything yourself when
you clean up but you kind of have to do that anyway (for structs).

hth,
H.

Signature

To reply via email, replace "LastNameHere" with my last name.

Frederick Cheung - 05 Feb 2004 09:07 GMT
> pSomeOtherStruct = (SomeStruct *)::CFArrayGetValueAtIndex( someArray,
> somevalidIndex );
>
> you'll have to iterate the array and deallocate everything yourself when
> you clean up but you kind of have to do that anyway (for structs).

Not necessarily, the callbacks you provide when you create the array can
handle that for you.

Fred
Herb Petschauer - 05 Feb 2004 18:42 GMT
In article
<Pine.LNX.4.44.0402050906420.10579-100000@kern.srcf.societies.cam.ac.uk>
,

> > pSomeOtherStruct = (SomeStruct *)::CFArrayGetValueAtIndex( someArray,
> > somevalidIndex );
[quoted text clipped - 6 lines]
>
> Fred

True enough (but I often forget!).

Signature

To reply via email, replace "LastNameHere" with my last name.

Mike Lesser - 06 Feb 2004 18:53 GMT
[[ This message was both posted and mailed: see
  the "To," "Cc," and "Newsgroups" headers for details. ]]

> Is it posible to use a CFMutableArray to store structs? ... can
> someone show me an example, please?

As eveyrone else has already pointed out, CFArrays store a 4-byte
value, so it's not necessarily a great idea. The STL solution is
probably better in many respects (e.g, less work, less resources).

In order to do what you want in a CF-savvy way, you'd have to create
other reference-based objects, including other CF types. You don't
necesarily have to use something you allocated with new.

For example, a struct can easily be turned into a generic CFData object
and stuck into the array. You can also try a dictionary. I have one
project where time is not so critical, but the ability to modify the
Dictionary contents at a much later time _is_. There are also lost of
examples where the OS returns CFArrays of CFDicts. In my experience, a
CFArray of CFStrings can be wicked excellent for a particular set of
circumstances. I've done a lot of this in the recent past, and it's
pretty easy.

Certainly most instances of CFArrays of objects is too much work, and
too much overhead, but you have to weigh the pros and cons.  There's no
single "right" way.

Signature

remove the obvious to reply by email

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.