• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: newbie confused by obj-c syntax
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie confused by obj-c syntax


  • Subject: Re: newbie confused by obj-c syntax
  • From: "John C. Randolph" <email@hidden>
  • Date: Tue, 15 Mar 2005 15:27:17 -0800


On Mar 15, 2005, at 4:30 AM, Clark Cox wrote:

On Mon, 14 Mar 2005 22:54:55 -1000, Daniel Child <email@hidden> wrote:
Hi All,

I am trying to use my own homegrown array of a class I created, called
StrokeDescription. This class has two member variables: desc
(description) and strokeType.


Now I want to have a class that stores an array of these StrokeType
objects. Somehow this didn't seem so hard in Java. I am getting hung up
in Objective-C syntax and was hoping you could help out.


Three questions:
1) Is it better to always use NSArray and NSMutableArray classes,
rather than create your own classes of homegrown class objects.

It might not *always* be better, but I'd say that in this case, it would be better and less error-prone for you.

2) How do you return an array (and not lose it).

In C, C++ and Objective-C, when you pass an array as a parameter or return it from a function (or use it in most other expressions for that matter), it decays into a pointer to the first element. So, when you try, in two places, to return an array, you're really returning a pointer to a local variable. That local variable ceases to exist once the method returns, so you have an invalid pointer.

The common ways to get around this are:
1) Allocate the array dynamically via malloc(). This has the downside
that it is the caller's responsibility to free the memory (i.e. it
isn't ref-counted or garbage collected). Like so:

int *FunctionReturningArray()
{
  /*Allocate enough memory to hold an array of 50 ints*/
  int *myArray = malloc(50 * sizeof *myArray);

  /*Populate the array*/
  for(...)
  {
    myArray[i] = ...;
  }

  /*Caller is responsible for calling free() in the result, or else
you've got a memory leak*/
  return myArray;
}

2) Have the caller pass in an array for you to populate. In this case,
you must be careful that the caller passes an array that is large
enough for your needs, so that you do not run off it's end. Like so:

bool FunctionPopulatingArray(int *myArray, size_t arraySize)
{
  if(arraySize < whataeveSizeIReallyNeed)
  {
    /*Array is too small*/
    return false;
  }

  /*Populate the array*/
  for(...)
  {
    myArray[i] = ...;
  }
  return true;
}

3) In Objective-C, just use NSArray (as it is refcounted, you can just
return an autorelease-ed instance)

Let me add one more option to Clark's suggestions.. You can also use an NSData object to pass an arbitrary number of bytes around. Instead of malloc(), you can use [NSMutableData dataWithCapacity:] to allocate the data object, and then use -mutableBytes to get a void* to the data you've asked for.


-jcr


John C. Randolph <email@hidden> (408) 974-8819 Sr. Cocoa Software Engineer, Apple Worldwide Developer Relations http://developer.apple.com/cocoa/index.html



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >newbie confused by obj-c syntax (From: Daniel Child <email@hidden>)
 >Re: newbie confused by obj-c syntax (From: Clark Cox <email@hidden>)

  • Prev by Date: Re: Dynamically resizable window
  • Next by Date: Re: multidimensional arrays
  • Previous by thread: Re: newbie confused by obj-c syntax
  • Next by thread: Re: newbie confused by obj-c syntax
  • Index(es):
    • Date
    • Thread