@ ответ Игоря Тандетника сработал
#include<windows.h>
int main() {
LPVOID ptr = VirtualAlloc(NULL,3000,MEM_RESERVE,PAGE_READWRITE); //reserving memory
ptr = VirtualAlloc(ptr,3000,MEM_COMMIT,PAGE_READWRITE); //commiting memory
memset(ptr, 'a', 3000);
VirtualFree(ptr, 0, MEM_RELEASE); //releasing memory
return 0;
}
спасибо за ваш ответ.
[править] также пробовал приводить к char *, и это тоже работает.
Еще раз спасибо.
#include<windows.h>
int main() {
int i;
LPVOID ptr = VirtualAlloc(NULL,3000,MEM_RESERVE,PAGE_READWRITE); //reserving memory
ptr = VirtualAlloc(ptr,3000,MEM_COMMIT,PAGE_READWRITE); //commiting memory
char* char_ptr = static_cast<char*>(ptr);
for(i=0;i<3000;i++){
char_ptr[i] = 'a';
}
VirtualFree(ptr, 0, MEM_RELEASE); //releasing memory
return 0;
}
[править] [2]
Обработка возвращаемых значений как @DavidHeffernan sugested:
#include<windows.h>
#include<iostream>
using namespace std;
int main() {
size_t in_num_of_bytes,i;
cout<<"Please enter the number of bytes you want to allocate:";
cin>>in_num_of_bytes;
LPVOID ptr = VirtualAlloc(NULL,in_num_of_bytes,MEM_RESERVE | MEM_COMMIT,PAGE_READWRITE); //reserving and commiting memory
if(ptr){
char* char_ptr = static_cast<char*>(ptr);
for(i=0;i<in_num_of_bytes;i++){ //write to memory
char_ptr[i] = 'a';
}
for(i=0;i<in_num_of_bytes;i++){ //print memory contents
cout<<char_ptr[i];
}
VirtualFree(ptr, 0, MEM_RELEASE); //releasing memory
}else{
cout<<"[ERROR]:Could not allocate "<<in_num_of_bytes<<" bytes of memory"<<endl;
}
return 0;
}