CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2018

    HeapAlloc memory manipulation


    I am learning about WinApi in C++ from Jamsa's book ( don't judge me about the age of the book ) and I am having problems with HeapAlloc.
    More specific:

    I have extracted from the program the following declarations:

    #define BLOCKSIZE 32
    static LPSTR *lpStrList;
    static HANDLE hHeap=NULL;
    My problem is Here:

    hHeap = HeapCreate(0 ,1024, 0);
    lpStrList= HeapAlloc(hHeap, HEAP_ZERO_MEMORY,  sizeof(LPSTR)*BLOCKSIZE);
    lpStrList is LPSTR tipe, and HeapAlloc returns LPVOID type, and the compiler don't let me to go further, it says:

    error C2440: '=' : cannot convert from 'LPVOID' to 'LPTSTR'
    I don't understand where is the problem, I have changed the setting unicode - multibyte character set and that didn't solved nothing.
    I have succesfully compiled and runned all the examples presented in the book until now, I want to resolve that problem and go further.

    Help me please with some clues to make this work or tell me where is the problem and why the prgram was structured in that way

    Thank you,

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Hanover Germany

    Re: HeapAlloc memory manipulation

    HeapAlloc just allocates some block of memory, it has no idea what type of data is about to be stored there,
    It's up to you to cast the returned LPVOID pointer to the type you need. Like
    LPSTR lpStrList= (LPSTR)HeapAlloc(hHeap, HEAP_ZERO_MEMORY,  sizeof(LPSTR)*BLOCKSIZE);
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2018

    Re: HeapAlloc memory manipulation

    Thank you very much for the logigue behind the explanation.
    Because the lpStrList was originally declaret as a pointer, I keeped that, and I had casted in that mode:

    LPSTR  *lpStrList = (*LPSTR)HeapAlloc(hHeap, HEAP_ZERO_MEMORY,  sizeof(LPSTR)*BLOCKSIZE);
    The error has disappeared, I hope that the functionality to be the same.

    Thank you very much for your reponse,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width