• 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: NSRectArray by reference
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSRectArray by reference


  • Subject: Re: NSRectArray by reference
  • From: Brian Stern <email@hidden>
  • Date: Sun, 9 Sep 2007 17:19:32 -0400

You have an extra dereference is several places. To fix see 3 notes below

CalendarRowRects(calendarRect, calendarInfo.totalRows, &rowRects); // note: remove the &

The function in question -

NS_INLINE void CalendarRowRects(NSRect calendarRect, NSUInteger rows, NSRectArray *rowRects) { // note: remove the *
(*rowRects[index]) = currentRowRect; // note: remove the outer parens and the *


Nevermind, I've become a little obsessed with passing and returning arguments by reference lately. I've renamed the function to CreateCalendarRowRects and simply malloc'ing the memory in there then returning the array pointer.

The problem with this solution is that you will be mixing Obj-C memory management policies with your ad hoc C/malloc memory policy. While it is certainly possible to do this correctly it may be confusing to someone who looks at the code in a year or two (even you). It may result in a memory leak.


A few suggestions:

* Use the NSArray solution that you have rejected. You didn't mention how many rows there are but if it's less than say 100 there will not be much overhead.

* Use an NSMutableData instance to hold the rects. There will be less Obj-C overhead in this case than the NSArray solution.

* Make the data block that holds the NSRects a data member and make the CalendarRowRects function a method. Reallocate the memory block as required each time the CalendarRowRects method is called. Release the memory block in your object's dealloc method. This has the benefit of being very simple and straightforward and makes memory errors unlikely. It will also have relatively low overhead.

--
Brian Stern
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


  • Follow-Ups:
    • Re: NSRectArray by reference
      • From: Uli Kusterer <email@hidden>
References: 
 >NSRectArray by reference (From: Keith Duncan <email@hidden>)

  • Prev by Date: Re: Cocoa with CodeWarrior?
  • Next by Date: Re: Closing a window with a textview using keyboard
  • Previous by thread: Re: NSRectArray by reference
  • Next by thread: Re: NSRectArray by reference
  • Index(es):
    • Date
    • Thread