网站首页  词典首页

请输入您要查询的函数:

 

术语 localalloc
释义 LocalAlloc
语法:
C++
HLOCAL WINAPI LocalAlloc(
__in UINT uFlags,
__in SIZE_T uBytes
);
LocalAlloc函数
分配指定的字节数从堆中。
注意当地的职能,并提供更大的开销比其他内存管理功能较少的功能。新的应用程序应该使用堆文档指出,除非当地的职能应常用的功能。有关更多信息,请参阅全局和局部的功能。
参数
uFlags [in]
内存分配的属性。默认为LMEM_FIXED价值。此参数可以是一个或多个下列值除特别指出的是不相容的组合。
ValueMeaning
LHND
0x0042Combines LMEM_MOVEABLE和LMEM_ZEROINIT。
LMEM_FIXED
0x0000Allocates固定的内存。返回值是对内存对象的指针。
LMEM_MOVEABLE
0x0002Allocates移动内存。内存块是从来没有搬迁的物理内存中,但它们可以在默认堆感动。
返回值是一个句柄内存对象。为了把该处理的一个指针,使用LocalLock功能。
此值不能结合LMEM_FIXED。
LMEM_ZEROINIT
0x0040Initializes内存内容为零。
LPTR
0x0040Combines LMEM_FIXED和LMEM_ZEROINIT。
NONZEROLHNDSame的LMEM_MOVEABLE。
NONZEROLPTRSame的LMEM_FIXED。
下面的值是过时的,但与16位Windows兼容。它们将被忽略。
LMEM_DISCARDABLE
LMEM_NOCOMPACT
LMEM_NODISCARD
uBytes [in]
的字节数分配。如果这个参数是零,uFlags参数指定LMEM_MOVEABLE,该函数返回的句柄内存对象标记为丢弃。
返回值
如果函数成功,返回值是一个句柄到新分配的内存对象。
如果函数失败,返回值为NULL。为了获得更多错误信息,调用GetLastError。
备注
Windows的内存管理不提供单独的本地堆和全局堆。因此,LocalAlloc和GlobalAlloc功能基本相同。
可移动内存标志LHND,LMEM_MOVABLE和NONZEROLHND增加不必要的开销,并要求锁定安全使用。他们应该避免,除非文件明确规定,他们应该被使用。
新的应用程序应该使用的文件堆,除非特别说明,一个局部函数应该使用功能。例如,某些Windows功能分配内存,必须以LocalFree释放。
如果堆没有提供足够的自由空间来满足要求,LocalAlloc返回NULL。因为NULL是用来表示错误,虚拟地址零从未拨出。它因此,很难探测到的一个NULL指针的使用。
至少金额如果LocalAlloc函数成功,它分配的要求。如果拨款额大于所要求的数额,这个过程可以使用全部款项。要确定分配的实际字节数,使用LocalSize功能。
要释放内存,使用LocalFree功能。这是不安全的自由与LocalAlloc分配的内存使用GlobalFree。
实例
下面的代码显示了LocalAlloc和LocalFree简单使用。
#include
#include
void _cdecl main()
{
LPTSTR pszBuf=NULL;
pszBuf = (LPTSTR)LocalAlloc(
LPTR,
MAX_PATH*sizeof(TCHAR));
// Handle error condition
if( pszBuf == NULL )
{
printf("LocalAlloc failed (%d)\\n", GetLastError());
return;
}
 
 //see how much memory was allocated
 printf("LocalAlloc allocated %d bytes\\n", LocalSize(pszBuf));

 // Use the memory allocated
 
// Free the memory when finished with it
LocalFree(pszBuf);
}
要求:
最低支持:client-Windows 2000专业版
最低支持server-Windows 2000服务器
HeaderWinbase.h(头文件:winuser.h)
LibraryKernel32.lib
DLLKernel32.dll
参见
全局和局部功能
堆函数
LocalFree
LocalLock
LocalReAlloc
LocalSize
内存管理功能
如果有任何问题和意见,请发送给微软(wsddocfb@microsoft.com)
生成日期:2009年8月27日
==英文原文==LocalAlloc Function
Allocates the specified number of bytes from the heap.
Note The local functions have greater overhead and provide fewer features than other memory management functions. New applications should use the heap functions unless documentation states that a local function should be used. For more information, see Global and Local Functions .
Syntax
C++
HLOCAL WINAPI LocalAlloc(
__in UINT uFlags,
__in SIZE_T uBytes
);
Parameters
uFlags [in]
The memory allocation attributes. The default is the LMEM_FIXED value. This parameter can be one or more of the following values, except for the incompatible combinations that are specifically noted.
ValueMeaning
LHND
0x0042Combines LMEM_MOVEABLE and LMEM_ZEROINIT.
LMEM_FIXED
0x0000Allocates fixed memory. The return value is a pointer to the memory object.
LMEM_MOVEABLE
0x0002Allocates movable memory. Memory blocks are never moved in physical memory, but they can be moved within the default heap.
The return value is a handle to the memory object. To translate the handle to a pointer, use the LocalLock function.
This value cannot be combined with LMEM_FIXED.
LMEM_ZEROINIT
0x0040Initializes memory contents to zero.
LPTR
0x0040Combines LMEM_FIXED and LMEM_ZEROINIT.
NONZEROLHNDSame as LMEM_MOVEABLE.
NONZEROLPTRSame as LMEM_FIXED.

The following values are obsolete, but are provided for compatibility with 16-bit Windows. They are ignored.
LMEM_DISCARDABLE
LMEM_NOCOMPACT
LMEM_NODISCARD
uBytes [in]
The number of bytes to allocate. If this parameter is zero and the uFlags parameter specifies LMEM_MOVEABLE, the function returns a handle to a memory object that is marked as discarded.
Return Value
If the function succeeds, the return value is a handle to the newly allocated memory object.
If the function fails, the return value is NULL. To get extended error information, call GetLastError .
Remarks
Windows memory management does not provide a separate local heap and global heap. Therefore, the LocalAlloc and GlobalAlloc functions are essentially the same.
The movable-memory flags LHND, LMEM_MOVABLE, and NONZEROLHND add unnecessary overhead and require locking to be used safely. They should be avoided unless documentation specifically states that they should be used.
New applications should use the heap functions unless the documentation specifically states that a local function should be used. For example, some Windows functions allocate memory that must be freed with LocalFree .
If the heap does not contain sufficient free space to satisfy the request, LocalAlloc returns NULL. Because NULL is used to indicate an error, virtual address zero is never allocated. It is, therefore, easy to detect the use of a NULL pointer.
If the LocalAlloc function succeeds, it allocates at least the amount requested. If the amount allocated is greater than the amount requested, the process can use the entire amount. To determine the actual number of bytes allocated, use the LocalSize function.
To free the memory, use the LocalFree function. It is not safe to free memory allocated with LocalAlloc using GlobalFree .
Examples
The following code shows a simple use of LocalAlloc and LocalFree.
#include
#include
void _cdecl main()
{
LPTSTR pszBuf=NULL;
pszBuf = (LPTSTR)LocalAlloc(
LPTR,
MAX_PATH*sizeof(TCHAR));
// Handle error condition
if( pszBuf == NULL )
{
printf("LocalAlloc failed (%d)\\n", GetLastError());
return;
}
 
 //see how much memory was allocated
 printf("LocalAlloc allocated %d bytes\\n", LocalSize(pszBuf));

 // Use the memory allocated
 
// Free the memory when finished with it
LocalFree(pszBuf);
}
Requirements
Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
See Also
Global and Local Functions
Heap Functions
LocalFree
LocalLock
LocalReAlloc
LocalSize
Memory Management Functions
Send comments about this topic to Microsoft
Build date: 8/27/2009
==原始网址==http://msdn.microsoft.com/en-us/library/aa366723(VS.85).aspx\n
随便看

 

windows api函数参考手册包含2258条windows api函数文档,详细介绍nodejs、java、rust调用windows api的方法技巧,是学习windows api编程的入门中文文档。

 

Copyright © 2004-2023 Winrtm.com All Rights Reserved
京ICP备2021023879号-40 更新时间:2024/10/6 13:20:20