Solved

"GlobalAlloc" versus "malloc" versus "new"

Posted on 2004-08-31
6
861 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

626 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