-
November 7th, 2021, 01:38 PM
#1
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,
-
November 8th, 2021, 03:34 AM
#2
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
-
November 8th, 2021, 12:45 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|