虚拟分配的问题
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 和 virtualpointer 是指向 char 的指针。我首先保留空间,用虚拟指针指向该空间。然后我将另外 2 个指针设置为等于虚拟指针。然后,我使用级数(现在也指向保留空间)提交该空间的 (1<<15),然后递增级数指针。然后我尝试在 mainhashbuf 应该指向的现在提交的空间中设置一个值,但是我遇到了写入异常。我是否错误地使用了 virtualalloc 和/或对指针的实际工作方式有错误的概念?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VirtualAlloc 尝试分配连续范围的虚拟页。 1<<31 == 0x80000000,这是 Windows 上用户模式进程默认拥有的内存量。我严重怀疑对第一个 VirtualAlloc 的调用是否成功。
选择较低的值并重新开始。另外,为什么要使用 VirtualAlloc?当您刚接触指针和直接内存管理时,以页面大小的单位提交和保留的概念可能有点令人畏惧。首先尝试使用 malloc/HeapAlloc 吗?另外,检查 VirtualAlloc 的返回值并确保它们不为 NULL。
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.