From: alien.0101 on
Hi Guys,

Can someone tell me that how LocalAlloc LocalFree Works?

If I do something like

LPBYTE *pByte= (LPBYTE) LocalAlloc(LPTR, dwSize);


and later in code for freeing memory I do

Localfree(pByte)

then how would Localfree knows how much memory to free, since I am
just passing pointer to memory location.
--> Does while allocating Localalloc marks the start and end of memory
region by some marker?
--> Or compiler / OS do some bookkeeping and store information like
mem pointer---> size.

any ideas???
From: Jeroen Mostert on
alien.0101(a)gmail.com wrote:
> Can someone tell me that how LocalAlloc LocalFree Works?
>
> If I do something like
>
> LPBYTE *pByte= (LPBYTE) LocalAlloc(LPTR, dwSize);
>
>
> and later in code for freeing memory I do
>
> Localfree(pByte)
>
> then how would Localfree knows how much memory to free, since I am
> just passing pointer to memory location.
> --> Does while allocating Localalloc marks the start and end of memory
> region by some marker?

No, that would be impossible. What marker would be unique? You could store
any bytes you wanted to in the block.

> --> Or compiler / OS do some bookkeeping and store information like
> mem pointer---> size.
>
It's this one. Exactly how the bookkeeping is done shouldn't concern you,
since it's an implementation detail and subject to change, but imagine that
information on how large the region is is stored in bytes that are just
before the allocated region, so the system just has to look a bit before
your pointer to find out.

--
J.
http://symbolsprose.blogspot.com
From: alien.0101 on
On Jun 20, 1:04 pm, Jeroen Mostert <jmost...(a)xs4all.nl> wrote:
> alien.0...(a)gmail.com wrote:
> > Can someone tell me that how LocalAlloc LocalFree Works?
>
> > If I do something like
>
> > LPBYTE *pByte=   (LPBYTE) LocalAlloc(LPTR, dwSize);
>
> > and later in code for freeing memory I do
>
> > Localfree(pByte)
>
> > then how would Localfree knows how much memory to free, since I am
> > just passing pointer to memory location.
> > --> Does while allocating Localalloc marks the start and end of memory
> > region by some marker?
>
> No, that would be impossible. What marker would be unique? You could store
> any bytes you wanted to in the block.
>
> > --> Or compiler / OS do some bookkeeping and store information like
> > mem pointer---> size.
>
> It's this one. Exactly how the bookkeeping is done shouldn't concern you,
> since it's an implementation detail and subject to change, but imagine that
> information on how large the region is is stored in bytes that are just
> before the allocated region, so the system just has to look a bit before
> your pointer to find out.
I need to know how its done...as I am getting debug error.. and not
able to figure out.

Here is the link to actual problem..
http://groups.google.com/group/comp.os.ms-windows.programmer.win32/browse_thread/thread/28bc129fcfcf1111

>
> --
> J.http://symbolsprose.blogspot.com

From: Christian ASTOR on
alien.0101(a)gmail.com wrote:

> Can someone tell me that how LocalAlloc LocalFree Works?
>
> If I do something like
>
> LPBYTE *pByte= (LPBYTE) LocalAlloc(LPTR, dwSize);
>
>
> and later in code for freeing memory I do
>
> Localfree(pByte)
>
> then how would Localfree knows how much memory to free, since I am
> just passing pointer to memory location.
> --> Does while allocating Localalloc marks the start and end of memory
> region by some marker?

Sizes and offsets are stored in the HEAP_ENTRY structure
http://www.nirsoft.net/kernel_struct/vista/HEAP_ENTRY.html
(LocalAlloc() calls RtlAllocateHeap() on Peb->ProcessHeap (which is
GetProcessHeap()))
(and Flags member is marked as busy (HEAP_ENTRY_BUSY))