|
-
Dec 5th, 2011, 10:36 AM
#1
Thread Starter
Lively Member
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.
-
Dec 5th, 2011, 12:04 PM
#2
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
Last edited by LaVolpe; Dec 5th, 2011 at 12:21 PM.
-
Dec 5th, 2011, 01:53 PM
#3
Thread Starter
Lively Member
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 ?
-
Dec 5th, 2011, 02:33 PM
#4
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
-
Dec 5th, 2011, 04:28 PM
#5
Thread Starter
Lively Member
Re: GlobalAllock, GlobalLock...What's the Deal ?
Thanks for the clarification.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|