Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"GlobalAlloc" versus "malloc" versus "new"

Posted on 2004-08-31
6
Medium Priority
?
880 Views
Last Modified: 2013-12-03
I have a large program dating back to Windows 3.1, and as such it contains many GlobalAllocs.  Recently the locking and unlocking of memory has gotten a bit cumbersome and I'd like to replace it.

Unfortunately the program is rather large, and I can't just say "it's getting hard to keep track of what memory handles are locked in this spagetti-code" to management.  Is there a good reason to not use it?  It seems under Win32 it offers no real advantage (backwards compatability is not an issue -- it won't even run under Win95 anymore) and way too much hassle.

I will also accept "For shame, GlobalAlloc is much better because . . ." arguments, provided the ". . ." is filled in.

Thanks.
0
Comment
Question by:KurtVon
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Cayce
ID: 11947414
0
 
LVL 9

Expert Comment

by:Cayce
ID: 11947525
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11949416
Is the memory shared between multiple programs? It would take some effort to recreate that behavior without GlobalAlloc.

Are the blocks resized? If so, the program may be taking advantage of the fact that the handle does not change when reallocations occur. It would take some effort to recreate that behavior without using GlobalAlloc.

Does the program rely on GlobalSize to get the size of a memory block? Again, it would take some effort to recreate that behavior without using GlobalAlloc.

If you don't share memory blocks with other running programs, and you don't rely on reallocations not affecting the handle value, and you don't need to get the size of memory blocks, you are open to easily use alternatives.

For allocations smaller than one megabyte, the heap functions are a good choice. For larger allocations, use VirtualAlloc and VirtualFree using MEM_COMMIT/PAGE_READWRITE and MEM_RELEASE respectively.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/heap_functions.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/virtual_memory_functions.asp

As an added bonus, when using the heap functions, you can create separate heaps for certain areas of the program. When you destroy a heap, it automatically deallocates all blocks inside that heap, without having to worry about freeing each block inside that heap.

Since the program is constantly locking and unlocking the blocks, you may want to create a couple of "wrapper" functions that take the same parameters as GlobalAlloc/GlobalFree/GlobalLock/GlobalUnlock, but instead of returning a handle, it just returns a pointer to the memory block. Then, your "GlobalLock" (maybe call it MyGlobalLock) function would just return the "handle" unmodified, because it is already a pointer. MyGlobalUnlock would do nothing. GlobalAlloc/GlobalFree would pass thru to the heap functions or whatever you want it to really use, and would return the pointer to the actual block instead of a handle.

Again, the above trick would work great, ONLY IF the code does not expect the handle to stay the same when a reallocations occur.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 11

Author Comment

by:KurtVon
ID: 11952668
The program uses the GlobalAlloc functions just like a malloc.  It is used to store static image data that is never resized, and whose dimensions are stored in the image height and width vars and is only shared with some dlls.  As far as the program is concerned, all these memory allocation techniques are equivalent.

Compiler and runtime dependancy aren't a problem since the whole project is already in VC++ anyway.  That isn't going to change.  All allocations are at least  half a meg, with most running to 40MB.

One possible way of handling the Windows Alloc functions without needing to keep track of them is to define a CGlobalAlloc object that takes the alloced handle as a constructor.  Add a pointer operator that locks the handle the first time it is called and a destructor that releases the handle.  It would prevent non-freed handles from becoming a problem.  I already do this with critical sections, but it is a great deal of work if a GlobalAlloc is not needed in the first place.

I also note this comment: "The following are the global and local functions. These functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE and the clipboard functions."

So the GlobalAllocs should or shouldn't be used?
0
 
LVL 9

Accepted Solution

by:
Cayce earned 200 total points
ID: 11952933
Consider this:

How often do you allocate/deallocate?
What memory blocks do you use for DDE functions?
How big are your allocated memory blocks?

If you allocate/deallocate very often you will gain some benefit by using the Heap* functions instead.
If your memory blocks are very big and persistent you can use the Virtual* functions instead.
If you need DDE/clipboard functions, you're stuck with the Global* functions.
0
 
LVL 11

Author Comment

by:KurtVon
ID: 11953404
Nope, no DDE or clipboard, and allocs are relatively infrequent.  The only reason the GlobalAllocs are there is because under VC1.0 the malloc function was limited to 64K.

Okay, thanks.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question