Something similar to 'GlobalAlloc' in Objective-C

Discussion in 'Mac Programming' started by anni.saini, Dec 23, 2009.

  1. anni.saini macrumors newbie

    Joined:
    Jul 15, 2009
    #1
    Can we allocate a global memory to particular object or data which can be shared to all the application as available 'GlobalAlloc' in the cpp.
     
  2. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #2
    http://msdn.microsoft.com/en-us/library/aa366596(VS.85).aspx

    So far as I can tell from skimming that, this is a Win-16 convention. You can just use "malloc" and "free," as defined by C.

    It also seems from reading the documentation that you don't understand what it did to begin with. If you weren't programming for Windows 3.1, it didn't do anything special. It calls HeapAlloc and HeapFree, which are also just overly complicated forms of "malloc" and "free." (For some bizarre reason, Microsoft thought it would be a good idea to allow programs to create more than one heap.) The original GlobalAlloc allowed you to allocate memory that was available to all processes. As of Win32 (that would be Windows 95), it doesn't anymore.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    The way the cool kids share data across processes on UNIX and UNIX-alike systems (and even windows) is POSIX shared memory segments. I doubt very seriously that you'd be able to get an actual Objective-C object created in shared memory, copied there, etc. but you could serialize objects into primitive types, a struct, etc. and stick it into shared memory so it can be shared between processes. You will need to deal with locking (likely in the form of semaphores) to handle access to the shared area.

    Look at the man pages for:
    shmat, shmctl, shmdt, shm_open, shm_unlink, shmget

    And:
    http://www.opengroup.org/onlinepubs/007908799/xsh/ipc.html

    for details.

    The way the really cool kids do this is using Distributed Objects.

    -Lee
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    If you are wanting to use the same instance of a class all over the place you should make a singleton.

    http://en.wikipedia.org/wiki/Singleton_pattern#Objective_C

    The idea behind a singleton is to have a class method that returns the same instance of that class every time you call the method.... and the very first time you call the method it creates that instance...

    On a side note, in a lot of things I do I'll add categories to some of Apple's classes that seem like they could benefit from having app wide instances like NSNumberFormatter and NSDateFormatter.

    Here is my category for NSNumberFormatter that accomplishes this for me.
    (I'm well aware that I have done nothing to keep it from getting released, so it isn't a true singleton like the shared UIApplication)

    Code:
    @interface NSNumberFormatter (Shared) 
    + (NSNumberFormatter *) sharedNSNumberFormatter;
    @end
    @implementation NSNumberFormatter (Shared)
    + (NSNumberFormatter *) sharedNSNumberFormatter {
    	static NSNumberFormatter *shared = nil;
    	
    	if (!shared) {
    		shared = [[NSNumberFormatter alloc] init];
    	}
    	
    	return shared;
    }
    @end
    
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    But singletons live in the app space, by themselves they would be of little use for crossing app boundaries.
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    I read it as the OP wanting to use the same object anywhere in a single app. My bad.
     
  7. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #7
  8. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #8
    You people are reading way too much into this. Unless the OP is programming for Win16, GlobalAlloc does not create shared memory. Under Windows 95 and later, it does virtual the same thing as malloc--it allocates memory on the heap. Period. End of story unless the OP comes back and says that he is still programming for a pseudo operating system that died fifteen years ago.
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    emphasis is mine. "Shared between applications" sounds like shared memory to me... But the OP doesn't sound like a native English speaker, so it's a matter of interpretation at this point.

    -Lee
     
  10. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #10
    Shared to all the application (singular) would simply be malloc. I agree that the OP does not sound like a native English speaker, but there's no way in hell he's expecting anything to behave like Win16. I have no reason whatsoever to believe he wants to share memory between applications.
     
  11. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    Or a singleton if you want to share an instance of a class.
     
  12. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #12
    :eek: Shoot, thats helpful.
     
  13. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
  14. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #15
    Oops! I see Lee and I were thinking along the same line, and I upset Detrius. :eek:

    For this thread to have further use I think the OP will have to add information.

    I apologize for adding nothing this time around. :(
     

Share This Page