GetProcessGroupAffinity因GroupArray未4字节对齐返回ERROR_NOACCESS,是否有文档说明?
GetProcessGroupAffinity要求[out] GroupArray内存过度对齐:是否为Bug及相关说明
问题概述
GetProcessGroupAffinity函数要求其[out] PUSHORT GroupArray参数指向的内存需满足过度对齐(超出USHORT类型的自然对齐要求),这一情况是否属于Bug?是否有官方文档记录?
现有提及
目前仅在fio项目的Windows CPU亲和性代码注释中找到相关说明:
/* * GetProcessGroupAffinity() seems to expect more than the natural * alignment for a USHORT from the area pointed to by current_groups so * arrange for maximum alignment by allocating via malloc() */
复现代码
#include <windows.h> #include <iostream> int main() { USHORT GroupCount = 1; alignas(4) USHORT GroupArray[2]; auto status = GetProcessGroupAffinity(GetCurrentProcess(), &GroupCount, GroupArray); auto a = GetLastError(); std::cout << a << ' ' << status << '\n'; status = GetProcessGroupAffinity(GetCurrentProcess(), &GroupCount, GroupArray + 1); a = GetLastError(); std::cout << a << ' ' << status << '\n'; }
执行结果
执行命令:g++ a.cpp -static && ./a
输出内容:
0 1 998 0
说明
微软官方文档未明确标注GetProcessGroupAffinity对GroupArray的对齐要求,这属于未被文档记录的实现细节。从复现结果来看,当内存不满足足够对齐时,函数会返回错误(错误码998代表无效内存访问)。
若要规避该问题,建议为GroupArray分配满足最大对齐要求的内存,比如使用malloc分配,或显式指定alignas(4)及以上的对齐属性。
内容的提问来源于stack exchange,提问作者Sopel
相关产品推荐
相关产品推荐

