C + + LocalAlloc () & Localsize () & LocalFree ()

Source: Internet
Author: User
Tags windows 7 x64

For LocalAlloc function, refer to: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366723 (v=vs.85). aspx

For localsize function, refer to: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366745 (v=vs.85). aspx

For LocalFree function, refer to: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730 (v=vs.85). aspx

The following code is excerpted from: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366723 (v=vs.85). aspx

Ide:code::blocks 16.01

Operating system: Windows 7 x64

1#include <windows.h>2#include <stdio.h>3#include <tchar.h>4 5 int_cdecl _tmain ()6 {7LPTSTR Pszbuf =NULL;8 9Pszbuf =(LPTSTR) LocalAlloc (Ten Lptr, OneMAX_PATH *sizeof(TCHAR)); A  -     //Handle Error Condition -     if(Pszbuf = =NULL) the     { -_tprintf (TEXT ("LocalAlloc failed (%ld) \ n"), GetLastError ()); -         return 1; -     } +  -     //See how much memory is allocated +_tprintf (TEXT ("LocalAlloc Allocated%d bytes\n"), Localsize (PSZBUF)); A  at     //Use the memory allocated -  -     //Free the memory when finished with it - LocalFree (PSZBUF); -  -     return 0; in}

Running results in Code::Blocks 16.01:

Ide:microsoft Visual Studio Community 2017 15.5.2

Operating system: 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 {9LPTSTR Pszbuf =NULL;Ten  OnePszbuf =(LPTSTR) LocalAlloc ( A Lptr, -MAX_PATH *sizeof(TCHAR)); -  the     //Handle Error Condition -     if(Pszbuf = =NULL) -     { -_tprintf (TEXT ("LocalAlloc failed (%d) \ n"), GetLastError ()); +         return 1; -     } +  A     //See how much memory is allocated at_tprintf (TEXT ("LocalAlloc Allocated%d bytes\n"), Localsize (PSZBUF)); -  -     //Use the memory allocated -  -     //Free the memory when finished with it - LocalFree (PSZBUF); in  - GetChar (); to  +     return 0; -}

Running results in Microsoft Visual Studio Community 2017 15.5.2:

The result of two IDE runs is different because TCHAR is defined differently in two Ides.

In Code::Blocks 16.01, TCHAR is defined as:

typedef CHAR TCHAR;
Char CHAR;

In Microsoft Visual Studio Community 2017 15.5.2, this is the case:

typedef wchar_t     TCHAR;

About Lptr, in Code::Blocks 16.01, is defined as:

#define LPTR 64

In Microsoft Visual Studio Community 2017 15.5.2, this is the case:

#define Lptr                (lmem_fixed | Lmem_zeroinit)
#define Lmem_fixed          0x0000
#define Lmem_zeroinit       0x0040

The two are equivalent, but the definition of Microsoft Visual Studio Community 2017 15.5.2 is clearer and at a glance.

About Lmem_fixed:allocates FIXED memory. The return value is a pointer to the memory object.

About lmem_zeroinit:initializes memory contents to zero.

C + + LocalAlloc () & Localsize () & LocalFree ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.