好的,所以我正在做一些涉及键盘输入的东西.我现在有一个像这样的巨大功能:
return key == BB_KEY_SPACE ||
key == BB_KEY_ZERO ||
key == BB_KEY_ONE ||
key == BB_KEY_TWO ||
key == BB_KEY_THREE ||
key == BB_KEY_FOUR ||
key == BB_KEY_FIVE ||
key == BB_KEY_SIX ||
key == BB_KEY_SEVEN ||
key == BB_KEY_EIGHT ||
key == BB_KEY_NINE ||
key == BB_KEY_A ||
key == BB_KEY_B ||
key == BB_KEY_C ||
key == BB_KEY_D ||
key == BB_KEY_E ||
key == BB_KEY_F ||
key == BB_KEY_G ||
key == BB_KEY_H ||
key == …Run Code Online (Sandbox Code Playgroud) 所以我一直在忙着编写自己的自定义内存分配器,但是我遇到了一些我不理解的奇怪行为.
考虑以下代码:
void* PointerUtil::AlignForward(void* address, const uint8_t& alignment)
{
return (void*)(((uintptr_t)(address) + (uintptr_t)(alignment - 1)) & (uintptr_t)(~(alignment - 1)));
}
Run Code Online (Sandbox Code Playgroud)
这应该采用指针和对齐要求,并修改指针使其在正向(正向)上正确对齐.
但是,当我这样测试时:
int address = 1240;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 512) << std::endl;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 256) << std::endl;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 128) << std::endl;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 64) << std::endl;
std::cout << (uintptr_t)memory::PointerUtil::AlignForward((void*)((uintptr_t)address), 32) << std::endl;
Run Code Online (Sandbox Code Playgroud)
我得到的输出是这样的:
0
0
1280
1280
1248
Run Code Online (Sandbox Code Playgroud)
这似乎不对.它应该是:
1536
1280
1280
1280
1248
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?