zoukankan      html  css  js  c++  java
  • C++ LocalAlloc() & LocalSize() & LocalFree ()

    关于LocalAlloc function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa366723(v=vs.85).aspx

    关于LocalSize function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa366745(v=vs.85).aspx

    关于LocalFree function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730(v=vs.85).aspx

    以下代码摘自:https://msdn.microsoft.com/en-us/library/windows/desktop/aa366723(v=vs.85).aspx

    IDE: Code::Blocks 16.01

    操作系统:Windows 7 x64

     1 #include <windows.h>
     2 #include <stdio.h>
     3 #include <tchar.h>
     4 
     5 int _cdecl _tmain()
     6 {
     7     LPTSTR pszBuf = NULL;
     8 
     9     pszBuf = (LPTSTR)LocalAlloc(
    10         LPTR,
    11         MAX_PATH * sizeof(TCHAR));
    12 
    13     // Handle error condition
    14     if (pszBuf == NULL)
    15     {
    16         _tprintf(TEXT("LocalAlloc failed (%ld)
    "), GetLastError());
    17         return 1;
    18     }
    19 
    20     //see how much memory was allocated
    21     _tprintf(TEXT("LocalAlloc allocated %d bytes
    "), LocalSize(pszBuf));
    22 
    23     // Use the memory allocated
    24 
    25     // Free the memory when finished with it
    26     LocalFree(pszBuf);
    27 
    28     return 0;
    29 }

    在Code::Blocks 16.01中的运行结果:

    IDE: Microsoft Visual Studio Community 2017 15.5.2

    操作系统:Windows 7 x64

     1 #include "stdafx.h"
     2 
     3 #include <windows.h>
     4 //#include <stdio.h>    
     5 //#include <tchar.h>
     6 
     7 int _cdecl _tmain()
     8 {
     9     LPTSTR pszBuf = NULL;
    10 
    11     pszBuf = (LPTSTR)LocalAlloc(
    12         LPTR,
    13         MAX_PATH * sizeof(TCHAR));
    14 
    15     // Handle error condition
    16     if (pszBuf == NULL)
    17     {
    18         _tprintf(TEXT("LocalAlloc failed (%d)
    "), GetLastError());
    19         return 1;
    20     }
    21 
    22     //see how much memory was allocated
    23     _tprintf(TEXT("LocalAlloc allocated %d bytes
    "), LocalSize(pszBuf));
    24 
    25     // Use the memory allocated
    26 
    27     // Free the memory when finished with it
    28     LocalFree(pszBuf);
    29 
    30     getchar();
    31 
    32     return 0;
    33 }

     在Microsoft Visual Studio Community 2017 15.5.2中的运行结果:


    造成两个IDE的运行结果不一样的原因,是因为TCHAR在两个IDE中的定义不一样。

    在Code::Blocks 16.01中,TCHAR是这样定义的:

    typedef CHAR TCHAR;
    typedef char CHAR;

     而在Microsoft Visual Studio Community 2017 15.5.2中,则是这样的:

    typedef wchar_t     TCHAR;

    关于LPTR,在Code::Blocks 16.01中,是这样定义的:

    #define LPTR 64

    而在Microsoft Visual Studio Community 2017 15.5.2中,则这样:

    #define LPTR                (LMEM_FIXED | LMEM_ZEROINIT)
    #define LMEM_FIXED          0x0000
    #define LMEM_ZEROINIT       0x0040

    两者是等效的,不过Microsoft Visual Studio Community 2017 15.5.2的定义则比较清晰,一目了然。

    关于LMEM_FIXED:Allocates fixed memory. The return value is a pointer to the memory object.

    关于LMEM_ZEROINIT:Initializes memory contents to zero.

  • 相关阅读:
    Python的异常处理
    flex stage.width 与stage.stageWidth的区别
    Flex timer使用 keydown事件注册到stage
    flex 事件注册和鼠标拖动
    window.open a.href打开窗口referer的问题
    java UUID的创建
    flex chrome浏览器调试flex程序
    EL表达式 requestScope initParam用法
    Java8 Lumbda表达式 初步
    jQuery 动画的执行
  • 原文地址:https://www.cnblogs.com/Satu/p/8213674.html
Copyright © 2011-2022 走看看