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

    HeapAlloc memory manipulation

    Hy,

    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:

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

    Code:
    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
    Location
    Hanover Germany
    Posts
    20,111

    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
    Code:
    LPSTR lpStrList= (LPSTR)HeapAlloc(hHeap, HEAP_ZERO_MEMORY,  sizeof(LPSTR)*BLOCKSIZE);
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2018
    Posts
    4

    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:

    Code:
    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

Featured