problem with virtualalloc

1.9k views Asked by At
virtualpointer=(char*) VirtualAlloc (NULL, (unsigned __int64) (1<<31), MEM_RESERVE, PAGE_READWRITE); 
mainhashbuf=progression=virtualpointer;
VirtualAlloc (progression, (unsigned __int64) (1<<15), MEM_COMMIT, PAGE_READWRITE);
progression=progression+capacity;
*(mainhashbuf+1000)='c';

mainhashbuf, progression and virtualpointer are pointers to char. I first reserve space, with virtual pointer pointing to that space. Then i set the other 2 pointers equal to virtual pointer. I then commit (1<<15) of that space using progression (which by now is also pointing to the reserve space), and then increment the progression pointer. Then I try to set a value in that now committed space which mainhashbuf SHOULD be pointing to, however i get a writing exception. Am I using virtualalloc wrong and/or have a wrong conception of how pointers actually work?

1

There are 1 answers

1
munin On BEST ANSWER

VirtualAlloc attempts to allocate a contiguous range of virtual pages. 1<<31 == 0x80000000, which is the amount of memory user-mode processes have on windows by default. I severely doubt that the call to the first VirtualAlloc is succeeding.

Choose lower values and start again. Also, why are you using VirtualAlloc? When you're new to pointers and direct memory management the concepts of committing and reserving in page-sized units can be a little daunting. Try working with malloc/HeapAlloc first? Also, check return values from VirtualAlloc and ensure they're non-NULL.