kam@iti.org (Keith A. McNabb) (06/04/91)
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. uSoft SDK documentation says only: "You can use ... malloc, calloc, realloc, and free, but [...] you may get unexpected results." ?????????? Keith McNabb kam@iti.org
svirsky@ttidca.TTI.COM (Bill Svirsky) (06/05/91)
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
bobsc@microsoft.UUCP (Bob SCHMIDT) (06/07/91)
In article <1991Jun3.195715.24598@iti.org> 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. %% %% uSoft SDK documentation says only: "You can use ... malloc, calloc, %% realloc, and free, but [...] you may get unexpected results." %% %% ?????????? %% %% Keith McNabb %% kam@iti.org There is a better way... The Windows API 'LocalHandle' will convert a LocalLocked pointer to its corresponding local handle. Thus, you can create your own 'Free' which is essentially HANDLE Free(void _near *Pointer) { return LocalFree(LocalHandle(Pointer)); } This code is naive and has no error checking, but does show the idea. -- -- Bob Schmidt bobsc@microsoft.UUCP -- -- Bellevue WA USA Windows SDK Support -- Sydney NSW Australia Developer Support (after 1 Oct)