Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

anni.saini

macrumors newbie
Original poster
Jul 15, 2009
18
0
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.
 
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.
 
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
 
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
 
But singletons live in the app space, by themselves they would be of little use for crossing app boundaries.
 
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.
 
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.

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
 
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.
 
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.

Or a singleton if you want to share an instance of a class.
 
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
:eek: Shoot, thats helpful.
 
facepalm.jpg
 
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. :(
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.