CreateThread
分配的栈空间是否会干扰VirtualAlloc
的使用?我找不到任何讨论或文档来准确解释允许分配堆栈空间的位置......
以下更准确地说明了我的问题:
uint8_t *baseA = (uint8_t*)VirtualAlloc(NULL,1,MEM_RESERVE,PAGE_NOACCESS);
// Create a thread with the default stack size
HANDLE hThread = CreateThread(NULL,0,SomeThreadProc,NULL,NULL,NULL);
// Possibly create even more threads here.
// Can this ever fail in the absence of other allocators? It doesn't here...
uint8_t *baseB = (uint8_t*)VirtualAlloc(NULL,1,MEM_RESERVE,PAGE_NOACCESS);
// Furthermore, in this test, baseB-baseA == 65536 (unless the debugger did something),
// so nothing appeared between baseA and baseB... not even enough space for the
// full 64kb of wastage, as baseA points to 4096 bytes by itself
如果它确实实际上使用了一些类似VirtualAlloc
的东西,有没有办法改变Windows在给定进程中分配堆栈空间的方式?
最佳答案
栈空间可以分配在进程地址空间的任何地方。现在没有这方面的文档,将来也不太可能出现这样的文档。
您可以安全地假设线程的创建和虚拟分配是独立的。如果不是这样,很多东西都会坏掉。分配器不能给出重叠的地址范围。这是不可想象的。问题出在其他地方。
唯一可能看起来像相关性的东西 - 使用的内存量和虚拟地址空间碎片。在这种情况下,最新的请求将简单地失败。
我从事内存分析实用程序的工作。
此图显示了每个分配大小的虚拟分配数量分布。
这是 32 位进程的地址空间内容示例(蓝色 - 已提交,洋红色 - 保留,绿色是空闲内存)。
我在这里写的是基于一个真实的经历。
关于c++ - CreateThread 会干扰 VirtualAlloc 的使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11911303/