Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!linus!philabs!ttidca!svirsky From: svirsky@ttidca.TTI.COM (Bill Svirsky) Newsgroups: comp.windows.ms Subject: Re: Does free() Work On Locked Pointers? Message-ID: <26652@ttidca.TTI.COM> Date: 5 Jun 91 16:01:44 GMT References: <1991Jun3.195715.24598@iti.org> Distribution: na Organization: Citicorp/TTI, Santa Monica Lines: 37 kam@iti.org (Keith A. McNabb) writes: > Does anyone know if the the regular C free() function works in Windows > 3.0 *IF* you've previously allocated memory with LocalAlloc() and then > obtained the pointer with LocalLock()? > > I'm modifying some existing C code, and I don't want to have to keep up > with handles; it's just a linked list, so the memory blocks are tiny. > I'm hoping that I can free memory for freed list elements with the > regular free function, passing it the pointer . LocalFree() requires a > handle. Yes, you can for the most part use the standard calloc, malloc, realloc, and free functions, if you stay away from compact and large memory models. The startup code in Windows programs maps these functions to equivalent Local/GlobalAlloc functions. If you use LMEM_FIXED in LocalAlloc, the handle returned is actually a near pointer. In fact, windows.h defines a special macro for this purpose; LPTR ("local pointer") is defined as (LMEM_FIXED | LMEM_ZEROINIT). You can use it as follows: pStr = (char NEAR *)LocalAlloc(LPTR, wSize); To release it, just cast the pointer to a handle and call LocalFree: LocalFree((LOCALHANDLE)pStr); The above is also true for GlobalAlloc/Free. All of the above info, in more detail, is found in Petzold's book, "Programming Windows", a good book to have. I couldn't find it in the Microsoft's "Windows Programmer's Reference". -- Bill Svirsky, Citicorp+TTI, 3100 Ocean Park Blvd., Santa Monica, CA 90405 Work phone: 213-450-9111 x2597 svirsky@ttidca.tti.com | ...!{csun,psivax,rdlvax,retix}!ttidca!svirsky