when would you use allocWithZone?

Discussion in 'Mac Programming' started by Chirone, Nov 14, 2010.

  1. macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #1
    just wondering when you would use this method over just your normal alloc

    i saw the method being used in some of apples sample code related to NSMenu
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    When physical locality in memory for different objects matters. This is probably rare in practice, but if you have a long-running program, it may be important to keep things close together to avoid swapping with closely-related objects. If these objects are far apart physically in memory it may require swapping to access both of them close together temporally.

    -Lee
     
  3. thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #3
    So it's for performance reasons then...

    would you suggest it be used for menus?
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I could imagine it being useful for menus, as they will be used together and stick around for the life of your program. I'd keep it simple for now, but if one were to use this, menus might be a good place.

    -Lee
     
  5. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #5
    Zones appear to be largely non-useful. This is what CocoaDev has to say about them. The biggest drawback to zones is that you cannot apparently set the default zone, and if you use allocWithZone on an object that creates other objects, most or all of those other objects will almost certainly be allocated in the default zone.
     

Share This Page