Solved

"GlobalAlloc" versus "malloc" versus "new"

Posted on 2004-08-31
6
830 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
Comment Utility
0
 
LVL 9

Expert Comment

by:Cayce
Comment Utility
0
 
LVL 8

Expert Comment

by:adg080898
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Author Comment

by:KurtVon
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now