LocalAlloc和LocalRealloc用法 [英] LocalAlloc and LocalRealloc usage

查看:1128
本文介绍了LocalAlloc和LocalRealloc用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Visual Studio 2008 C ++ Windows Mobile 6应用程序,其中我使用FindFirst()/ FindNext()样式API来获取项目的集合。我不知道提前在列表中有多少项目。所以,我想动态分配这些项目的数组。

I have a Visual Studio 2008 C++ Windows Mobile 6 application where I'm using a FindFirst() / FindNext() style API to get a collection of items. I do not know how many items will be in the list ahead of time. So, I would like to dynamically allocate an array for these items.

通常,我会使用 std :: vector<> ,但由于其他原因,此应用程序的选项。所以,我使用 LocalAlloc() LocalReAlloc()

Normally, I would use a std::vector<>, but, for other reasons, that's not an option for this application. So, I'm using LocalAlloc() and LocalReAlloc().

我不清楚的是,如果这个内存应该被标记为固定或可移动。应用程序运行良好。我只是想知道什么是正确的。

What I'm not clear on is if this memory should be marked fixed or moveable. The application runs fine either way. I'm just wondering what's 'correct'.

int count = 0;
INFO_STRUCT* info = ( INFO_STRUCT* )LocalAlloc( LHND, sizeof( INFO_STRUCT ) );
while( S_OK == GetInfo( &info[ count ] )
{
    ++count;
    info = ( INFO_STRUCT* )LocalRealloc( info, sizeof( INFO_STRUCT ) * ( count + 1 ), LHND );
}

if( count > 0 )
{
    // use the data in some interesting way...
}

LocalFree( info );

b $ b PaulH

Thanks, PaulH

编辑:响应者(不是不合理地)在使用LocalAlloc更好的选项,所以我将提供更多的上下文。

Responders are (not unreasonably) getting hung up on the use of LocalAlloc() over other better options. So I will provide more context.

这段代码是从RAPI可调用的DLL中执行的,所以在这种情况下, :

This chunk of code is being executed from within a RAPI invokable DLL. So, in that context, it looks more like this:

FOO_API int RapiInvokable_Foo( DWORD /*cbInput*/, 
                               BYTE* /*pInput*/,
                               DWORD* pcbOutput, 
                               BYTE** ppOutput,
                               IRAPIStream* /*pStream*/ )
{
    int count = 0;
    INFO_STRUCT* info = ( INFO_STRUCT* )LocalAlloc( LPTR, sizeof( INFO_STRUCT ) );
    while( S_OK == GetInfo( &info[ count ] )
    {
        ++count;
        info = ( INFO_STRUCT* )LocalRealloc( info, sizeof( INFO_STRUCT ) * ( count + 1 ), LHND );
    }

    *ppOutput = ( BYTE* )info;
    *pcbOutput = sizeof( INFO_STRUCT ) * ( count + 1 );
    return S_OK;
}

CeRapiInvoke()文档:

应用程序应该使用LocalAlloc函数为pInput参数分配内存,调用者负责释放pInput,系统为ppOutput参数分配内存,当应用程序完成缓冲区时,使用LocalFree函数释放内存。

An application should allocate memory for the pInput parameter with the LocalAlloc function. The caller is responsible for freeing pInput. The system allocates memory for the ppOutput parameter. When the application is completed with the buffer, it should free the memory with the LocalFree function.

推荐答案

从我可以知道, LHND 甚至不是在 Windows Mobile中使用的有效标记版本 LocalAlloc

From what I can tell, LHND is not even a valid flag to use in the Windows Mobile version of LocalAlloc.

当您呼叫非移动版本 LocalAlloc LMEM_MOVEABLE ,返回类型为 INFO_STRUCT * 。返回类型是 HLOCAL - 一个句柄到你分配的内存。它不是一个指针本身,所以它是不正确的解引用它像一个指针。要获取指针,您需要使用 LocalLock 告诉操作系统它暂时不能移动内存。

When you call the non-mobile version of LocalAlloc with LMEM_MOVEABLE, the return type is not INFO_STRUCT*. The return type is HLOCAL — a handle to the memory that you've allocated. It's not a pointer itself, so it is incorrect to dereference it like a pointer. To get a pointer, you need to use LocalLock to tell the OS that it mustn't move the memory around for the time being.

请考虑 MSDN对可移动内存的说明: / p>

Consider what MSDN says about movable memory:


可移动存储器标志LHND,LMEM_MOVABLE和NONZEROLHND增加了不必要的开销,需要锁定才能安全使用。

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.

因此,如果你真的必须使用 LocalAlloc ,然后分配固定内存,不可移动。这是你从调用纯旧 malloc 时得到的相同行为。

So, if you really must use LocalAlloc, then allocate fixed memory, not movable. That's the same behavior you'd get from calling plain old malloc.

LMEM_MOVEABLE flag表示与 LocalReAlloc 不同的东西。而 LocalAlloc 它指定内存是否被锁定, LocalReAlloc 它指定是否允许该函数移动内存以便满足对更大的存储器块的请求。如果你不用 LocalReAlloc 包含那个标志,那么这个函数就限制在原地改变块的大小。如果没有空间,那么函数将失败,即使堆中其他地方有更大的内存块。

The LMEM_MOVEABLE flag means something different with LocalReAlloc. Whereas with LocalAlloc it specifies whether the memory is locked, with LocalReAlloc it specifies whether the function is allowed to move the memory in order to satisfy a request for a larger block of memory. If you don't include that flag with LocalReAlloc, then the function is restricted to changing the block's size in-place. If there's no room there, then the function will fail, even if there are larger blocks of memory available elsewhere in the heap.

要获得<$ c $的效果c> malloc ,调用 LocalAlloc(LMEM_FIXED)。要获得 realloc 的效果,调用 LocalReAlloc(LMEM_MOVEABLE)

To get the effect of malloc, call LocalAlloc(LMEM_FIXED). To get the effect of realloc, call LocalReAlloc(LMEM_MOVEABLE). Include LMEM_ZEROINIT in either case if you wish.

在这两种情况下,都包括 LMEM_ZEROINIT 应该只使用文档具体说明您可以用于每个功能的标志。对于 LocalAlloc ,它没有提及 LMEM_MOVEABLE LocalReAlloc ,它没有提到 LPTR

One thing to take away from all this seems to be that you should only use the flags that the documentation specifically says you can use for each function. For LocalAlloc, it doesn't mention LMEM_MOVEABLE, and for LocalReAlloc, it doesn't mention LPTR.

这篇关于LocalAlloc和LocalRealloc用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆