GlobalAllock, GlobalLock...What's the Deal ?
Hi all,
Can anybody tell me a scenario where one *needs* to Allocate and lock a memory block/area before working with that memory block ? I often see code that allocates/block memory via the GlobalAllock & GlobalLock APIs before assigning data to variables. I am confused and would appreciate if anybody could shed some light on this.
Re: GlobalAllock, GlobalLock...What's the Deal ?
Welcome to the forums.
You should specify which language you are using.
This isn't a perfect answer, but researching functions you are using (i.e., APIs) helps. Many times it tells you whether memory should be allocated with GlobalAlloc, LocalAlloc or VirtualAlloc for example. GlobalLock is easy enough: If you don't know if the memory was allocated as moveable or not, then use it when in doubt. Otherwise if it was not created as moveable, you shouldn't need to lock the memory pointer.
Edited: Here are some things to read over
Comparing Memory Allocation Methods
Global and Local Functions
Windows Memory Management
Windows’s Virtual Address Space: A Short MSDN Story
Re: GlobalAllock, GlobalLock...What's the Deal ?
Hey thanks for the answer.
I am using VB6/VBA.
My point is why is there a need to allocate memory in the first place ? And in which scenarios it becomes necessary to allocate memory before storing data in it ?
Re: GlobalAllock, GlobalLock...What's the Deal ?
There is no single answer. If you are not using any APIs or other DLLs that require memory allocation, then the answer is no. VB does all this behind the scenes for you.
Now, if you are using APIs or DLLs, then as mentioned previously, you need to research the API by going to MSDN.com and looking it up. If it specifically says to allocate memory using a specific method, then you should probably do that. GlobalAlloc & LocalAlloc for example use the same memory space but are wrappers to the the HeapAlloc function. Since they are wrappers, they have different ways of accessing/referencing the memory. If an API tells you to use one or the other, it is most likely because that API was coded to use that specific wrapper
Re: GlobalAllock, GlobalLock...What's the Deal ?
Thanks for the clarification.